summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/MemoryInit/Pei/Source/WriteTraining/MrcWriteLeveling.c
blob: 8b716204e1645e0e768804bf9b3de6321b843bd5 (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
/** @file
  The write leveling flow is the first part of the write training.
  In this stage the memory controller needs to synchronize its DQS sending
  with the clock for each DRAM.  The DRAM can be put in a mode where for a
  write command it responds by sampling the clock using DQS and sending it
  back as the data.  The IO can receive this and tune the DQS alignment so
  it will appear in sync with the clock at the DRAM side.
  The following algorithm is used for the write leveling flow:

@copyright
  Copyright (c) 1999 - 2013 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement.

**/
#include "MrcWriteLeveling.h"

/**
@brief
  this function execute the Jedec write leveling Cleanup.
  Center TxDQS-CLK timing

  @param[in, out] MrcData - Include all MRC global data.

  @retval MrcStatus -  if it succeded return mrcSuccess
**/
MrcStatus
MrcJedecWriteLevelingCleanUp (
  IN OUT MrcParameters *const MrcData
  )
{
  const MRC_REUTAddress REUTAddress = {{0, 0, 0, 0},    // Start
                                       {0, 0, 0, 1023}, // Stop
                                       {0, 0, 0, 0},    // Order
                                       {0, 0, 0, 0},    // IncRate
                                       {0, 0, 0, 1}};   // IncValue
  const U8          DumArr[7] = {1, 1, 1, 1, 1, 1, 1};
  const U8          DqOffsetMax = 7;
  const S8          DqOffsets[7] = {0, -10, 10, -5, 5, -15, 15};
  const S8          Offsets[5] = {0, 1, -1, 2, 3};
  const U8          PMaskConst[8] = {0, 0, 1, 1, 1, 1, 0, 0};
  const U32         CleanUpSeeds[MRC_WDB_NUM_MUX_SEEDS] = {0xAAAAAA, 0xCCCCCC, 0xF0F0F0};
  const U32         NormalSeeds[MRC_WDB_NUM_MUX_SEEDS] = {0xA10CA1, 0xEF0D08, 0xAD0A1E};
  const MrcInput    *Inputs;
  const MrcDebug    *Debug;
  MrcOutput         *Outputs;
  MrcControllerOut  *ControllerOut;
  MrcChannelOut     *ChannelOut;
  MrcStatus         Status;
  U8                Channel;
  U8                Rank;
  U8                Byte;
  U8                offset;
  U8                ChBitMask;
  U8                RankMask;  // RankBitMask for both channels
  U8                ValidRankMask;
  U8                Pattern[4][2];
  U8                PMask[sizeof (PMaskConst)];
  U8                AllGood;
  U8                AllGoodLoops;
  U8                DqOffset;
  U8                RankDouble;
  U8                RankHalf;
  U8                RankMod2;
  U8                Start;
  S8                ByteOff[MAX_CHANNEL][MAX_SDRAM_IN_DIMM]; // Passing offset for each ch/byte.
  S8                ByteSum[MAX_CHANNEL]; // Sum of passing offsets for a ch
  S8                TargetOffset;
  U16               ByteMask;
  U16               ValidByteMask;
  U16               Result;
  U16               SkipMe;
  U16               BytePass[MAX_CHANNEL]; // Bit mask indicating which ch/byte has passed
  S16               GlobalByteOff;
  U32               CRValue;
  U32               Offset;
  U32               CRAddDelay[MAX_CHANNEL];
  S32               LocalOffset;
  BOOL              Done;
#ifdef ULT_FLAG
  BOOL              Lpddr;
#endif //ULT_FLAG
  MRC_WDBPattern    WDBPattern;

  MCHBAR_CH0_CR_REUT_CH_PAT_WDB_CL_MUX_CFG_STRUCT                 ReutChPatWdbClMuxCfg;
  DDRDATA0CH0_CR_TXTRAINRANK0_STRUCT                              CrTxTrainRank;
#ifdef MRC_DEBUG_PRINT
  U32               ErrLower[MAX_CHANNEL];
  U32               ErrUpper[MAX_CHANNEL];
#endif // MRC_DEBUG_PRINT

  //
  // Setup REUT Pattern
  // Use 0x00FFC33C pattern to keep DQ-DQS simple but detect any failures
  // Same Pattern as NHM/WSM
  //
  Inputs              = &MrcData->SysIn.Inputs;
  Outputs             = &MrcData->SysOut.Outputs;
  ControllerOut       = &Outputs->Controller[0];
  Debug               = &Inputs->Debug;
  Status              = mrcSuccess;
  LocalOffset         = 0;
  Done                = TRUE;
  Pattern[0][0]       = 0x00;
  Pattern[0][1]       = 0xFF;
  Pattern[1][0]       = 0xFF;
  Pattern[1][1]       = 0x00;
  Pattern[2][0]       = 0xC3;
  Pattern[2][1]       = 0x3C;
  Pattern[3][0]       = 0x3C;
  Pattern[3][1]       = 0xC3;
  WDBPattern.IncRate  = 1;
  WDBPattern.Start    = 0;
  WDBPattern.Stop     = 3;
  WDBPattern.DQPat    = BasicVA;
  MrcOemMemoryCpy (PMask, (U8 *) PMaskConst, sizeof (PMask));
  MrcOemMemorySet ((U8 *) CRAddDelay, 0, sizeof (CRAddDelay));
#ifdef MRC_DEBUG_PRINT
  MrcOemMemorySet ((U8 *) ErrLower, 0, sizeof (ErrLower));
  MrcOemMemorySet ((U8 *) ErrUpper, 0, sizeof (ErrUpper));
#endif // MRC_DEBUG_PRINT

#ifdef ULT_FLAG
  //
  // Check if LPDDR3 memory is used
  //
  Lpddr = (Outputs->DdrType == MRC_DDR_TYPE_LPDDR3);
#endif // ULT_FLAG
  //
  // Spread = 8, Start = 0, 1, 2 and 3
  //
  for (Start = 0; Start < (sizeof (Pattern) / sizeof (Pattern[0])); Start++) {
    WriteWDBFixedPattern (MrcData, Pattern[Start], PMask, 8, Start);
  }

  //
  // Set LSFR Seed to be sequential
  //
  MrcProgramLFSR (MrcData, CleanUpSeeds);

  //
  // Set Channel and Rank bit masks
  //
  ChBitMask     = Outputs->ValidChBitMask;
  ValidRankMask = Outputs->ValidRankMask;
  ValidByteMask = (MRC_BIT0 << Outputs->SdramCount) - 1; // 0x1FF or 0xFF
  //
  // Setip IO test CmdPat=PatWrRd, NumCL=4, LC=4, REUTAddress, SOE=3,
  // WDBPattern, EnCADB=0, EnCKE=0, SubSeqWait=0 )
  //
  SetupIOTest (MrcData, ChBitMask, PatWrRd, 2, 4, &REUTAddress, NSOE, &WDBPattern, 0, 0, 0);

  //
  // Progam BITBUFFER for JWLT
  //
  ReutChPatWdbClMuxCfg.Data                     = 0;
  ReutChPatWdbClMuxCfg.Bits.Mux0_Control        = BTBUFFER;
  ReutChPatWdbClMuxCfg.Bits.Mux1_Control        = BTBUFFER;
  ReutChPatWdbClMuxCfg.Bits.Mux2_Control        = BTBUFFER;
  ReutChPatWdbClMuxCfg.Bits.ECC_Data_Source_Sel = 1;
  for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
    if (MrcChannelExist (Outputs, Channel)) {
      Offset = MCHBAR_CH0_CR_REUT_CH_PAT_WDB_CL_MUX_CFG_REG +
        ((MCHBAR_CH1_CR_REUT_CH_PAT_WDB_CL_MUX_CFG_REG - MCHBAR_CH0_CR_REUT_CH_PAT_WDB_CL_MUX_CFG_REG) * Channel);
      MrcWriteCR (MrcData, Offset, ReutChPatWdbClMuxCfg.Data);
    }
  }

  for (Rank = 0; Rank < MAX_RANK_IN_CHANNEL; Rank++) {
    //
    // Select Rank for REUT test
    //
    ChBitMask   = 0;
    RankMask    = MRC_BIT0 << Rank;
    RankDouble  = Rank * 2;
    RankHalf    = Rank / 2;
    RankMod2    = Rank % 2;
    for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
      ChBitMask |= SelectReutRanks (MrcData, Channel, (RankMask), 0);
      BytePass[Channel] = ByteSum[Channel] = 0;
    }
    //
    // Skip if both channels empty
    //
    if (!(RankMask & ValidRankMask)) {
      continue;
    }

    MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\nRank %d\n", Rank);

    //
    // *************************************************
    // Sweep through the cycle offsets until we find a value that passes
    // *************************************************
    //
    MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "Sweep through the cycle offsets until we find a value that passes\n");

    if (RankMask & ValidRankMask) {
      MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "Channel                 0                1\nDelay  DqOffset   Byte \t");
      MRC_DEBUG_MSG (
        Debug,
        MSG_LEVEL_NOTE, (
        Outputs->SdramCount == MAX_SDRAM_IN_DIMM
        ) ? "0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8" : "0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7"
        );
    }

    for (offset = 0; offset < sizeof (Offsets); offset++) {
      //
      // Program new delay offsets to DQ/DQS timing:
      //
      /// @todo - Address debuging switches or remove.
      // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "Program new delay offsets to DQ/DQS timing %d\n", Offsets[offset]);
      //
      for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
        if (!(MrcRankInChannelExist (MrcData, Rank, Channel))) {
          continue;
        }

        ChannelOut = &ControllerOut->Channel[Channel];

        //
        // Calculate offsets
        //
        GlobalByteOff = 0;
        if (Offsets[offset] > MAX_ADD_DELAY) {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], MAX_ADD_DELAY, RankDouble, 2);
          GlobalByteOff       = 128 * (Offsets[offset] - MAX_ADD_DELAY);
        } else if (Offsets[offset] < 0) {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], 0, RankDouble, 2);
          GlobalByteOff       = 128 * Offsets[offset];
        } else {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], Offsets[offset], RankDouble, 2);
        }
        //
        // Write Tx DQ/DQS Flyby delays
        // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "Add GlobalByteOff = %d to TxDQS Flyby delay: Ch %d \n", GlobalByteOff, Channel);
        //
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          CRValue                       = ChannelOut->TxDqs[Rank][Byte] + GlobalByteOff;
          CrTxTrainRank.Data            = 0;
          CrTxTrainRank.Bits.TxDqDelay  = CRValue + 32;
          CrTxTrainRank.Bits.TxDqsDelay = CRValue;
          CrTxTrainRank.Bits.TxEqualization = ChannelOut->TxEq[Rank][Byte];
          UpdateTxT (MrcData, Channel, Rank, Byte, 0x3, CrTxTrainRank.Data);
          //
          /// @todo - Address debuging switches or remove.
          // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "CRValue = 0x%x \n", CrTxTrainRank.Data);
          //
        }
        //
        // Write Wr ADD Delays
        //
        Offset = MCHBAR_CH0_CR_SC_WR_ADD_DELAY_REG + ((MCHBAR_CH1_CR_SC_WR_ADD_DELAY_REG - MCHBAR_CH0_CR_SC_WR_ADD_DELAY_REG) * Channel);
        MrcWriteCR (MrcData, Offset, CRAddDelay[Channel]);
        //
        /// @todo - Address debuging switches or remove.
        // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "CRAddDelay[%d] = 0x%x \n", Channel, CRAddDelay[Channel]);
        //
      }

#ifdef ULT_FLAG
      if (!Lpddr) {
#endif // ULT_FLAG
        //
        // Reset FIFOs & Reset DRAM DLL (Micron WorkAround). Wait 1uS for test to complete
        //
        Status = IoReset (MrcData);

        for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
          if (MrcRankInChannelExist (MrcData, Rank, Channel)) {
            ChannelOut = &ControllerOut->Channel[Channel];
            Status = MrcWriteMRS (
                      MrcData,
                      Channel,
                      RankMask,
                      mrMR0,
                      ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR0] | (MRC_BIT0 << 8)
                      );
          }
        }

        MrcWait (MrcData, (1 * HPET_1US));
#ifdef ULT_FLAG
      }
#endif // ULT_FLAG

      //
      // Run Test across all DqOffsets points
      //
      for (DqOffset = 0; DqOffset < DqOffsetMax; DqOffset++) {
        //
        // Update Offset
        //
        ChangeMargin (MrcData, WrT, DqOffsets[DqOffset], 0, 1, 0, Rank, 0, 0, 0, 0, MrcRegFileRank);

        //
        // Run Test
        // DQPat = BasicVA, DumArr, ClearErrors = 1, mode = 0
        //
        RunIOTest (MrcData, ChBitMask, BasicVA, DumArr, 1, 0);

        //
        // Update results for all ch/bytes
        //
        Done = TRUE;

        MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\n% 3d\t% 3d\t       \t", Offsets[offset], DqOffsets[DqOffset]);

        //
        // Update results for all ch/bytes
        //
        for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
          if (!(MrcRankInChannelExist (MrcData, Rank, Channel))) {
            if (Channel == 0) {
              MRC_DEBUG_MSG (
                Debug,
                MSG_LEVEL_NOTE,
                (Outputs->SdramCount == MAX_SDRAM_IN_DIMM) ? "                 " : "                "
                );
            }

            continue;
          }
          //
          // Read out per byte error results and check for any byte error
          //
          Offset = 4 + MCHBAR_CH0_CR_REUT_CH_ERR_ECC_CHUNK_RANK_BYTE_NTH_STATUS_REG +
            (
              (
                MCHBAR_CH1_CR_REUT_CH_ERR_ECC_CHUNK_RANK_BYTE_NTH_STATUS_REG -
                MCHBAR_CH0_CR_REUT_CH_ERR_ECC_CHUNK_RANK_BYTE_NTH_STATUS_REG
              ) * Channel
            );
          Result = (U16) MrcReadCR (MrcData, Offset);
          SkipMe = (Result & ValidByteMask) | BytePass[Channel];

#ifdef MRC_DEBUG_PRINT
          Offset = MCHBAR_CH0_CR_REUT_CH_ERR_DATA_STATUS_REG + ((MCHBAR_CH1_CR_REUT_CH_ERR_DATA_STATUS_REG - MCHBAR_CH0_CR_REUT_CH_ERR_DATA_STATUS_REG) * Channel);
          ErrLower[Channel] = MrcReadCR (MrcData, Offset);
          //
          // Lower 32 bits
          //
          ErrUpper[Channel] = MrcReadCR (MrcData, Offset + 4);
          //
          // Upper 32 bits
          //
#endif // MRC_DEBUG_PRINT
          for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
            ByteMask = MRC_BIT0 << Byte;
            MRC_DEBUG_MSG (
              Debug,
              MSG_LEVEL_NOTE,
              ((Result & ValidByteMask) & ByteMask) ?
                "# " : //Fail
                ". " // Pass
              );
            //
            // If this byte has failed or previously passed, nothing to do
            //
            if (SkipMe & ByteMask) {
              continue;
            }

            BytePass[Channel] |= ByteMask;
            ByteOff[Channel][Byte] = Offsets[offset];
            ByteSum[Channel] += Offsets[offset];
          }
          //
          /// @todo - Address debuging switches or remove.
          // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "For DqOffsets %d: BytePass[%d] = 0x%X, Result = 0x%x, SkipMe = 0x%x\n", DqOffsets[DqOffset], Channel, BytePass[Channel], Result, SkipMe);
          //
          if (BytePass[Channel] != ValidByteMask) {
            Done = FALSE;
          }
        }

#ifdef MRC_DEBUG_PRINT
        for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
          if (MrcRankInChannelExist (MrcData, Rank, Channel)) {
            MRC_DEBUG_MSG (
              Debug,
              MSG_LEVEL_NOTE,
              "0x%08x%08x ",
              ErrUpper[Channel],
              ErrLower[Channel]
              );
          }
        }
#endif // MRC_DEBUG_PRINT
        //
        // Jump out of the for DqOffset loop if everybody is passing
        //
        if (Done == TRUE) {
          break;
        }
      }
      //
      // Jump out of the for offset loop if everybody is passing
      //
      if (Done == TRUE) {
        break;
      }
    }

    MRC_DEBUG_MSG (Debug, MSG_LEVEL_ERROR, "\n");

    //
    // Walk through and find the correct value for each ch/byte
    //
    for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
      if (!(MrcRankInChannelExist (MrcData, Rank, Channel))) {
        continue;
      }

      ChannelOut = &ControllerOut->Channel[Channel];

      if (Done == FALSE) {
        MRC_DEBUG_MSG (
          Debug,
          MSG_LEVEL_ERROR,
          "Error! Write Leveling CleanUp - Couldn't find a passing value for all bytes on Channel %u Rank %u:\nBytes - ",
          Channel,
          Rank
          );
#ifdef MRC_DEBUG_PRINT
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          MRC_DEBUG_MSG (Debug, MSG_LEVEL_ERROR, ((BytePass[Channel] ^ ValidByteMask) & (1 << Byte)) ? "%d " : "", Byte);
        }
        MRC_DEBUG_MSG (Debug, MSG_LEVEL_ERROR, "\n");
#endif
        return mrcWriteLevelingError;
      }
      //
      // Calculate the average offset, rounding up
      // Apply that offset to the global MC CRAddDelay register
      //
      TargetOffset = (ByteSum[Channel] + (S8) (Outputs->SdramCount / 2)) / (S8) Outputs->SdramCount;
      //
      /// @todo - Address debuging switches or remove.
      // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "TargetOffset = 0x%x, ByteSum[%d] = 0x%x \n", TargetOffset, Channel, ByteSum[Channel]);
      //
      AllGood       = 0;
      AllGoodLoops  = 0;
      GlobalByteOff = 0;
      while (AllGood == 0) {
        //
        // Update CRAddDelay and GlobalByteOff
        //
        GlobalByteOff = 0;
        if (TargetOffset > MAX_ADD_DELAY) {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], MAX_ADD_DELAY, RankDouble, 2);
          GlobalByteOff       = 128 * (TargetOffset - MAX_ADD_DELAY);
        } else if (TargetOffset < 0) {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], 0, RankDouble, 2);
          GlobalByteOff       = 128 * TargetOffset;
        } else {
          CRAddDelay[Channel] = MrcBitSwap (CRAddDelay[Channel], TargetOffset, RankDouble, 2);
        }
        //
        /// @todo - Address debuging switches or remove.
        // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "GlobalByteOff = 0x%x, CRAddDelay[%d] = 0x%x \n", GlobalByteOff, Channel, CRAddDelay[Channel]);
        // Refine TargetOffset to make sure it works for all byte lanes
        //
        AllGood = 1;
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          LocalOffset = GlobalByteOff + 128 * (ByteOff[Channel][Byte] - TargetOffset);
          if ((ChannelOut->TxDq[Rank][Byte] + LocalOffset) > (511 - 64)) {
            AllGood = 0;
            AllGoodLoops += 1;
            TargetOffset += 1;
            break;
          }

          if ((ChannelOut->TxDqs[Rank][Byte] + LocalOffset) < 96) {
            AllGood = 0;
            AllGoodLoops += 1;
            TargetOffset -= 1;
            break;
          }
          //
          /// @todo - Address debuging switches or remove.
          // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "TargetOffset = 0x%x, ByteOff[%d][%d] = 0x%x \n", TargetOffset, Channel, Byte, ByteOff[Channel][Byte]);
          //
        }
        //
        // Avoid an infinite loop
        //
        if (AllGoodLoops > 3) {
          MRC_DEBUG_MSG (Debug, MSG_LEVEL_ERROR, "Error Handler1: JWL CleanUp - TargetOffset refining failed \n");
          return mrcFail;
        }
      }

      MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "C%d.R%d:  Offset\tFinalEdge\n", Channel, Rank);
      //
      // Program the final settings to the DQ bytes and update MrcData
      //
      for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
        LocalOffset = GlobalByteOff + 128 * (ByteOff[Channel][Byte] - TargetOffset);
        ChannelOut->TxDq[Rank][Byte]  += (S16) LocalOffset;
        ChannelOut->TxDqs[Rank][Byte] += (S16) LocalOffset;
        UpdateTxT (MrcData, Channel, Rank, Byte, 0xFF, 0);
        MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "  B%d:   %d\t%d\n", Byte, LocalOffset, ChannelOut->TxDqs[Rank][Byte]);
      }
      //
      /// @todo - Address debuging switches or remove.
      // MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "LocalOffset = 0x%x \n", LocalOffset);
      // Update MC Delay
      //
      Offset = MCHBAR_CH0_CR_SC_WR_ADD_DELAY_REG + ((MCHBAR_CH1_CR_SC_WR_ADD_DELAY_REG - MCHBAR_CH0_CR_SC_WR_ADD_DELAY_REG) * Channel);
      MrcWriteCR (MrcData, Offset, CRAddDelay[Channel]);
      MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "CRAddDelay[%d] = 0x%x \n", Channel, CRAddDelay[Channel]);

#ifdef ULT_FLAG
      if (!Lpddr) {
#endif // ULT_FLAG
        //
        // Clean up after Test
        //
        Status = MrcWriteMRS (
                  MrcData,
                  Channel,
                  RankMask,
                  mrMR0,
                  ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR0] | (MRC_BIT0 << 8)
                  );
        MrcWait (MrcData, (1 * HPET_1US));
#ifdef ULT_FLAG
      }
#endif // ULT_FLAG
    }
  }
  //
  // Clean up after Test
  // Restore WDB - VicRot=8, Start=0 and restore default seed
  //
  WriteWDBVAPattern (MrcData, 0, BASIC_VA_PATTERN_SPRED_8, 8, 0);
  MrcProgramLFSR (MrcData, NormalSeeds);

  MrcWriteCrMulticast (MrcData, DDRDATA_CR_DDRCRDATAOFFSETTRAIN_REG, 0);
  Status = IoReset (MrcData);

  return Status;
}

/**
@brief
  this function execute the Jedec write leveling training.
  Center TxDQS-CLK timing

  @param[in, out] MrcData - Include all MRC global data.

  @retval MrcStatus -  if it succedes return mrcSuccess
**/
MrcStatus
MrcJedecWriteLevelingTraining (
  IN OUT MrcParameters *const MrcData
  )
{
  const MrcInput    *Inputs;
  const MrcDebug    *Debug;
  MrcOutput         *Outputs;
  MrcControllerOut  *ControllerOut;
  MrcChannelOut     *ChannelOut;
  TOdtValue         *OdtTableIndex;
  MrcStatus         Status;
  U8                Channel;
  U8                Rank;
  U8                RankDouble;
  U8                RankHalf;
  U8                RankMod2;
  U8                Byte;
  U8                refbyte;
  U8                ChBitMask;
  U8                RankMask;  // RankBitMask for both channels
  U8                ValidRankMask;
  U8                OtherDimm;
  U8                OdtMatrix;
  U16               WLStart;
  U16               WLStop;
  U16               WLDelay;
  U8                WLStep;
  U32               WaitTime;
  U32               CRValue;
  U32               Offset;
  U32               DqsToggleTime;
  S32               InitialPassingStart[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               InitialPassingEnd[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               CurrentPassingStart[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               CurrentPassingEnd[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               LargestPassingStart[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               LargestPassingEnd[MAX_CHANNEL][MAX_SDRAM_IN_DIMM];
  S32               iWidth;
  S32               cWidth;
  S32               lWidth;
  S32               ByteWidth;
  BOOL              Pass;
  BOOL              RankIsx16;
  BOOL              SavedRestoreMRS;
#ifdef ULT_FLAG
  BOOL              Lpddr;
#endif //ULT_FLAG

  DDR3_MODE_REGISTER_1_STRUCT                 Ddr3ModeRegister1;
  DDRDATACH0_CR_DDRCRDATACONTROL0_STRUCT      DdrCrDataControl0;
  DDRDATA0CH0_CR_DDRCRDATACONTROL2_STRUCT     DdrCrDataControl2;
  MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_STRUCT  ReutChMiscOdtCtrl;
  DDRDATA0CH0_CR_DDRCRDATACONTROL0_STRUCT     DdrCrData0Control0;
  DDRDATA0CH0_CR_DATATRAINFEEDBACK_STRUCT     DataTrainFeedback;

  Inputs        = &MrcData->SysIn.Inputs;
  Outputs       = &MrcData->SysOut.Outputs;
  ControllerOut = &Outputs->Controller[0];
  Debug         = &Inputs->Debug;
  Status        = mrcSuccess;
  OdtTableIndex = NULL;
  CRValue       = 0;
  ChBitMask     = Outputs->ValidChBitMask;
  ValidRankMask = Outputs->ValidRankMask;

  // Save the flag and force MRS recalculation
  SavedRestoreMRS = Outputs->RestoreMRs;
  Outputs->RestoreMRs = FALSE;

  DqsToggleTime = 1024;
#ifdef ULT_FLAG
  //
  // Check if LPDDR3 memory is used
  //
  Lpddr = (Outputs->DdrType == MRC_DDR_TYPE_LPDDR3);

  if (Lpddr) {
    DqsToggleTime = 2048;
  }
#endif // ULT_FLAG

  //
  // Enabling WLmode causes DQS to toggle for 1024 qclk.  Wait for this to stop
  // Round up to nearest uS
  //

  WaitTime = (Outputs->Qclkps * DqsToggleTime + 500000) / 1000000;
  //
  // Propagate delay values (without a write command) and Set Qoff on all ranks.
  //
  for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
    if (MrcChannelExist (Outputs, Channel)) {
      ChannelOut = &ControllerOut->Channel[Channel];

      //
      // Propagate delay values from rank 0 to prevent assertion failures in RTL
      //
      DdrCrDataControl0.Data            = ChannelOut->DqControl0.Data;
      DdrCrDataControl0.Bits.ReadRFRd   = 0;
      DdrCrDataControl0.Bits.ReadRFWr   = 1;
      DdrCrDataControl0.Bits.ReadRFRank = 0;
      Offset = DDRDATACH0_CR_DDRCRDATACONTROL0_REG + ((DDRDATACH1_CR_DDRCRDATACONTROL0_REG - DDRDATACH0_CR_DDRCRDATACONTROL0_REG) * Channel);
      MrcWriteCrMulticast (MrcData, Offset, DdrCrDataControl0.Data);

      //
      // Set ForceBiasOn and make sure ForceRxAmpOn is cleared
      //
      for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
        DdrCrDataControl2.Data              = ChannelOut->DqControl2[Byte].Data;
        DdrCrDataControl2.Bits.ForceBiasOn  = 1;
        DdrCrDataControl2.Bits.ForceRxOn    = 0;
#ifdef ULT_FLAG
        if (Lpddr) {
          DdrCrDataControl2.Bits.WlLongDelEn  = 1;
        }
#endif // ULT_FLAG
        Offset = DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG +
          ((DDRDATA0CH1_CR_DDRCRDATACONTROL2_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG) * Channel) +
          ((DDRDATA1CH0_CR_DDRCRDATACONTROL2_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG) * Byte);
        MrcWriteCR (MrcData, Offset, DdrCrDataControl2.Data);
      }
    }
  }

  for (Rank = 0; Rank < MAX_RANK_IN_CHANNEL; Rank++) {
    RankMask = MRC_BIT0 << Rank;
    if (!(RankMask & ValidRankMask)) {
      //
      // Skip if all channels empty
      //
      continue;
    }

    MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\nRank %d\n", Rank);
    RankDouble  = Rank * 2;
    RankHalf    = Rank / 2;
    RankMod2    = Rank % 2;
    //
    // Program MR1: Set A7 to enter Write Leveling mode
    // Write MaskRasWe to prevent scheduler from issuing non-Read commands
    //
    for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
      if (MrcRankInChannelExist (MrcData, Rank, Channel)) {

        ChannelOut = &ControllerOut->Channel[Channel];

#ifdef ULT_FLAG
        //
        // Enable WL mode in MR2[7]
        //
        if (Lpddr) {
          CRValue = (ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR2]);
          Status  = MrcIssueMrw (MrcData, Channel, Rank, mrMR2, (CRValue | MRC_BIT7), FALSE, FALSE);
        } else
#endif // ULT_FLAG
        {
          Ddr3ModeRegister1.Data = ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR1];
          Ddr3ModeRegister1.Bits.WriteLeveling = 1;

          OdtTableIndex = GetOdtTableIndex (MrcData, Channel, RANK_TO_DIMM_NUMBER (Rank));
          if (OdtTableIndex == NULL) {
            MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "Error: OdtTableIndex is NULL!\n");
            return mrcFail;
          }
#ifdef ULT_FLAG
          if (Inputs->CpuModel == cmHSW_ULT) {  // DDR3L case
            Ddr3ModeRegister1 = UpdateRttNomValue (MrcData, OdtTableIndex->RttWr, Ddr3ModeRegister1);
          }
#endif // ULT_FLAG

          //
          // In write leveling mode Rtt_Nom = Rtt_Wr ONLY for 2DPC
          //
          if (ChannelOut->DimmCount == 2) {
            Ddr3ModeRegister1 = UpdateRttNomValue (MrcData, OdtTableIndex->RttWr, Ddr3ModeRegister1);
          }
          Status = MrcWriteMRS (MrcData, Channel, RankMask, mrMR1, (U16) Ddr3ModeRegister1.Data);
        }

        //
        // Assert ODT for myself
        //
        OdtMatrix = RankMask;
        if (ChannelOut->DimmCount == 2) {
          //
          // Calculate non-target DIMM
          //
          OtherDimm = ((Rank + 2) / 2) & MRC_BIT0;
          //
          // Assert ODT for non-target DIMM
          //
          OdtMatrix |= 1 << (2 * OtherDimm);
        }

        ReutChMiscOdtCtrl.Data = 0;
#ifdef ULT_FLAG
        if (Lpddr) {
          //
          // Only one ODT pin for ULT
          //
          ReutChMiscOdtCtrl.Bits.ODT_On       = 1;
          ReutChMiscOdtCtrl.Bits.ODT_Override = 1;
        }
#endif // ULT_FLAG
#ifdef TRAD_FLAG
        if ((Inputs->CpuModel == cmHSW) || (Inputs->CpuModel == cmCRW)) {
          ReutChMiscOdtCtrl.Bits.ODT_On       = OdtMatrix;
          ReutChMiscOdtCtrl.Bits.ODT_Override = MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_ODT_Override_MAX;
        }
#endif // TRAD_FLAG
        Offset = MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_REG +
          ((MCHBAR_CH1_CR_REUT_CH_MISC_ODT_CTRL_REG - MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_REG) * Channel);
        MrcWriteCR (MrcData, Offset, ReutChMiscOdtCtrl.Data);
      }
    }  // for Channel

    //
    // ******************************************
    // STEP 1 and 2: Find middle of high region
    // ******************************************
    //
    WLStart = 192;
    WLStop  = 320;
    WLStep  = 2;

    MRC_DEBUG_MSG (
      Debug,
      MSG_LEVEL_NOTE,
      "\n\tCh0\t\t\t\t\t\t\t\t%sCh1\n",
      (Outputs->SdramCount == MAX_SDRAM_IN_DIMM) ? "\t" : ""
      );

    MRC_DEBUG_MSG (
      Debug,
      MSG_LEVEL_NOTE,
      "WLDelay%s%s",
      (Outputs->SdramCount == MAX_SDRAM_IN_DIMM) ? "\t0\t1\t2\t3\t4\t5\t6\t7\t8" : "\t0\t1\t2\t3\t4\t5\t6\t7",
      (Outputs->SdramCount == MAX_SDRAM_IN_DIMM) ? "\t0\t1\t2\t3\t4\t5\t6\t7\t8" : "\t0\t1\t2\t3\t4\t5\t6\t7"
      );

    for (WLDelay = WLStart; WLDelay < WLStop; WLDelay += WLStep) {
      MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\n %d:", WLDelay);
      //
      // Program WL DQS Delays:
      //
      for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
        if (MrcRankInChannelExist (MrcData, Rank, Channel)) {

          ChannelOut = &ControllerOut->Channel[Channel];

          //
          // Enable Write Level Mode in DDR and Propagate delay values (without a write command).
          // Stay in WLMode.
          //
          DdrCrData0Control0.Data                 = ChannelOut->DqControl0.Data;
          DdrCrData0Control0.Bits.WLTrainingMode  = 1;
          DdrCrData0Control0.Bits.TxPiOn          = 1;
          DdrCrData0Control0.Bits.ReadRFRd        = 0;
          DdrCrData0Control0.Bits.ReadRFWr        = 1;
          DdrCrData0Control0.Bits.ReadRFRank      = Rank;
          for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
            UpdateTxT (MrcData, Channel, Rank, Byte, 1, WLDelay);

            Offset = DDRDATA0CH0_CR_DDRCRDATACONTROL0_REG +
              ((DDRDATA0CH1_CR_DDRCRDATACONTROL0_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL0_REG) * Channel) +
              ((DDRDATA1CH0_CR_DDRCRDATACONTROL0_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL0_REG) * Byte);
            MrcWriteCR (MrcData, Offset, DdrCrData0Control0.Data);
          }
        }
      }

      if (WLDelay == WLStart) {
        MrcWait (MrcData, (WaitTime * HPET_1US));  // Wait for the first burst to finish
      }

      Status = IoReset (MrcData);

      MrcWait (MrcData, (WaitTime * HPET_1US));

      //
      // Update results for all Channels/Bytes
      //
      for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
        if (!MrcRankInChannelExist (MrcData, Rank, Channel)) {
          MRC_DEBUG_MSG (
            Debug,
            MSG_LEVEL_NOTE,
            "\t\t\t\t\t\t\t\t%s",
            (Outputs->SdramCount == MAX_SDRAM_IN_DIMM) ? "\t" : ""
            );
          continue;
        }
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          Offset = DDRDATA0CH0_CR_DATATRAINFEEDBACK_REG +
            ((DDRDATA0CH1_CR_DATATRAINFEEDBACK_REG - DDRDATA0CH0_CR_DATATRAINFEEDBACK_REG) * Channel) +
            ((DDRDATA1CH0_CR_DATATRAINFEEDBACK_REG - DDRDATA0CH0_CR_DATATRAINFEEDBACK_REG) * Byte);
          DataTrainFeedback.Data  = MrcReadCR (MrcData, Offset);
          Pass                    = (DataTrainFeedback.Bits.DataTrainFeedback >= 16);
          MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\t%c%d", Pass ? '.' : '#', DataTrainFeedback.Data);
          if (WLDelay == WLStart) {
            if (Pass) {
              InitialPassingStart[Channel][Byte] = InitialPassingEnd[Channel][Byte]  = WLStart;
              CurrentPassingStart[Channel][Byte] = CurrentPassingEnd[Channel][Byte]  = WLStart;
              LargestPassingStart[Channel][Byte] = LargestPassingEnd[Channel][Byte]  = WLStart;
            } else {
              InitialPassingStart[Channel][Byte] = InitialPassingEnd[Channel][Byte]  = -WLStep;
              CurrentPassingStart[Channel][Byte] = CurrentPassingEnd[Channel][Byte]  = -WLStep;
              LargestPassingStart[Channel][Byte] = LargestPassingEnd[Channel][Byte]  = -WLStep;
            }
          } else {
            if (Pass) {
              if (InitialPassingEnd[Channel][Byte] == (WLDelay - WLStep)) {
                InitialPassingEnd[Channel][Byte] = WLDelay;
              }

              if (CurrentPassingEnd[Channel][Byte] == (WLDelay - WLStep)) {
                CurrentPassingEnd[Channel][Byte] = WLDelay;
              } else {
                CurrentPassingStart[Channel][Byte] = CurrentPassingEnd[Channel][Byte] = WLDelay;
              }
              //
              // Special case for last step: Append Initial Passing Region
              // WLDelay should be considered a continuous range that wraps around 0
              //
              if ((WLDelay >= (WLStop - WLStep)) && (InitialPassingStart[Channel][Byte] == WLStart)) {
                iWidth = (InitialPassingEnd[Channel][Byte] - InitialPassingStart[Channel][Byte]);
                CurrentPassingEnd[Channel][Byte] += (WLStep + iWidth);
              }
              //
              // Update Largest variables
              //
              cWidth  = CurrentPassingEnd[Channel][Byte] - CurrentPassingStart[Channel][Byte];
              lWidth  = LargestPassingEnd[Channel][Byte] - LargestPassingStart[Channel][Byte];
              if (cWidth > lWidth) {
                LargestPassingStart[Channel][Byte]  = CurrentPassingStart[Channel][Byte];
                LargestPassingEnd[Channel][Byte]    = CurrentPassingEnd[Channel][Byte];
              }
            }
          }
        } // for Byte
      } // for Channel
    } // for WLDelay

#ifdef MRC_DEBUG_PRINT
    MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\n\tInitPassSt\tInitPassEn\tCurrPassSt\tCurrPassEn\tLargPassSt\tLargPassEn\n");

    for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
      if (MrcRankInChannelExist (MrcData, Rank, Channel)) {
        MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "C%d\n", Channel);
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          MRC_DEBUG_MSG (
            Debug,
            MSG_LEVEL_NOTE,
            "   B%d:\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
            Byte,
            InitialPassingStart[Channel][Byte],
            InitialPassingEnd[Channel][Byte],
            CurrentPassingStart[Channel][Byte],
            CurrentPassingEnd[Channel][Byte],
            LargestPassingStart[Channel][Byte],
            LargestPassingEnd[Channel][Byte]
            );
      }
    }
  }
#endif // MRC_DEBUG_PRINT

    //
    // Clean up after step
    // Very coarsely adjust for any cycle errors
    // Program values for TxDQS
    //
    for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
      if (MrcRankInChannelExist (MrcData, Rank, Channel)) {

        ChannelOut = &ControllerOut->Channel[Channel];

        //
        // Check if rank is a x16
        //
        RankIsx16 = (ChannelOut->Dimm[RankHalf].SdramWidth == 16 ? TRUE : FALSE);

        //
        // Clear ODT before MRS (JEDEC Spec)
        //
        Offset = MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_REG +
          ((MCHBAR_CH1_CR_REUT_CH_MISC_ODT_CTRL_REG - MCHBAR_CH0_CR_REUT_CH_MISC_ODT_CTRL_REG) * Channel);
        MrcWriteCR (MrcData, Offset, 0);

#ifdef ULT_FLAG
        //
        // Restore MR2 values
        //
        if (Lpddr) {
          CRValue = (ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR2]);
          Status  = MrcIssueMrw (MrcData, Channel, Rank, mrMR2, CRValue, FALSE, FALSE);
        } else
#endif // ULT_FLAG
        {
          //
          // Restore Write Leveling mode and Rtt_Nom for this rank
          //
          CRValue = (ChannelOut->Dimm[RankHalf].Rank[RankMod2].MR[mrMR1]);
          Status  = MrcWriteMRS (MrcData, Channel, RankMask, mrMR1, (U16) CRValue);
        }

        MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "C%d.R%d:  LftEdge Width\n", Channel, Rank);
        for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
          ByteWidth = LargestPassingEnd[Channel][Byte] - LargestPassingStart[Channel][Byte];
          MRC_DEBUG_MSG (
            Debug,
            MSG_LEVEL_NOTE,
            "  B%d:   %d\t%d\n",
            Byte,
            LargestPassingStart[Channel][Byte],
            ByteWidth
            );

          //
          // Check if width is valid
          //
          if ((ByteWidth <= 32) || (ByteWidth >= 96)) {
            MRC_DEBUG_MSG (
              Debug,
              MSG_LEVEL_ERROR,
              "\nERROR! Width region outside expected limits for Channel: %u Rank %u Byte: %u\n",
              Channel,
              Rank,
              Byte
              );
            return mrcWriteLevelingError;
          }
          //
          // Align byte pairs if DIMM is x16
          //
          if (RankIsx16 && (Byte & MRC_BIT0)) {
            //
            // If odd byte number (1, 3, 5 or 7)
            //
            refbyte = Byte - 1;
            if (LargestPassingStart[Channel][Byte] > (LargestPassingStart[Channel][refbyte] + 64)) {
              LargestPassingStart[Channel][Byte] -= 128;
            }

            if (LargestPassingStart[Channel][Byte] < (LargestPassingStart[Channel][refbyte] - 64)) {
              LargestPassingStart[Channel][Byte] += 128;
            }
          }

#ifdef ULT_FLAG
          if (Inputs->CpuModel == cmHSW_ULT) {
            //
            // Fix for b4618067 - need to add 1 QCLK to DqsPi
            //
            LargestPassingStart[Channel][Byte] += 64;
          }
#endif // ULT_FLAG

          ChannelOut->TxDqs[Rank][Byte] = (U16) LargestPassingStart[Channel][Byte];
          ChannelOut->TxDq[Rank][Byte]  = (U16) (LargestPassingStart[Channel][Byte] + 32);
          UpdateTxT (MrcData, Channel, Rank, Byte, 0xFF, 0);
        }
      }
    }
  }
  //
  // Clean up after Test
  //
  for (Channel = 0; Channel < MAX_CHANNEL; Channel++) {
    if (MrcChannelExist (Outputs, Channel)) {

      ChannelOut = &ControllerOut->Channel[Channel];

      Offset = DDRDATACH0_CR_DDRCRDATACONTROL0_REG +
        ((DDRDATACH1_CR_DDRCRDATACONTROL0_REG - DDRDATACH0_CR_DDRCRDATACONTROL0_REG) * Channel);
      MrcWriteCrMulticast (MrcData, Offset, ChannelOut->DqControl0.Data);

      //
      // Restore DqControl2 values.
      //
      for (Byte = 0; Byte < Outputs->SdramCount; Byte++) {
        Offset = DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG +
          ((DDRDATA0CH1_CR_DDRCRDATACONTROL2_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG) * Channel) +
          ((DDRDATA1CH0_CR_DDRCRDATACONTROL2_REG - DDRDATA0CH0_CR_DDRCRDATACONTROL2_REG) * Byte);
        MrcWriteCR (MrcData, Offset, ChannelOut->DqControl2[Byte].Data);
      }
    }
  }

#ifdef ULT_FLAG
  if (!Lpddr)
#endif // ULT_FLAG
  {
    //
    // DLLEnable=0, Dic=0, Al=0, Level=0, Tdqs=0, Qoff=0
    //
    Status = MrcSetMR1 (MrcData, 0, DIMMRON, 0, 0, 0, 0);
    if (Status != mrcSuccess) {
      MRC_DEBUG_MSG (Debug, MSG_LEVEL_ERROR, "ERROR: RESET FAIL - WLT\n");
      return Status;
    }
  }

  // Restore flag
  Outputs->RestoreMRs = SavedRestoreMRS;

  MRC_DEBUG_MSG (Debug, MSG_LEVEL_NOTE, "\nJedec Write Leveling CLEANUP\n");
  Status = MrcJedecWriteLevelingCleanUp (MrcData);

  return Status;
}