summaryrefslogtreecommitdiff
path: root/QuarkSocPkg/QuarkNorthCluster/MemoryInit/Pei/hte.c
blob: 92ec4ba1f682a54ad4a397ed4b37bf320556bfa0 (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
/** @file
HTE handling routines for MRC use.

Copyright (c) 2013-2015 Intel Corporation.

This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "mrc.h"
#include "memory_options.h"
#include "io.h"

#include "hte.h"


#ifdef SIM
VOID delay_n(UINT32 nanoseconds);
#define MySimStall(a)   delay_n(a/1000)
#endif

STATIC VOID EnableAllHteErrors(
    UINT8 Mask)
/*++

 Routine Description:

 This function enables to HTE to detect all possible errors for
 the given training parameters (per-bit or full byte lane).

 Returns:

 None

 --*/
{
  isbW32m(HTE, 0x000200A2, 0xFFFFFFFF);
  isbW32m(HTE, 0x000200A3, 0x000000FF);
  isbW32m(HTE, 0x000200A4, 0x00000000);
}

STATIC UINT32 CheckHteErrors(
    VOID)
/*++

 Routine Description:

 This function goes and reads the HTE register in order to find any error

 Returns:

 The errors detected in the HTE status register

 --*/
{
  return isbR32m(HTE, 0x000200A7);
}

STATIC VOID WaitForHteComplete(
    VOID)
/*++

 Routine Description:

 This function waits until HTE finishes

 Returns:

 None

 --*/
{
  UINT32 Tmp;

  ENTERFN();

  //
  // Is the test done?
  //
  do
  {
#ifdef SIM
    MySimStall (35000); // 35 ns delay
#endif
  } while (0 != (isbR32m(HTE, 0x00020012) & BIT30));

  Tmp = isbR32m(HTE, 0x00020011);
  Tmp = Tmp | BIT9;
  Tmp = Tmp & ~(BIT13 | BIT12);
  isbW32m(HTE, 0x00020011, Tmp);

  LEAVEFN();
}

STATIC VOID ClearHteErrorRegisters(
    VOID)
/*++

 Routine Description:

 Clears registers related with errors in the HTE.

 Returns:

 None

 --*/
{
  UINT32 Tmp;

  //
  // Clear all HTE errors and enable error checking
  // for burst and chunk.
  //
  Tmp = isbR32m(HTE, 0x000200A1);
  Tmp |= BIT8;
  isbW32m(HTE, 0x000200A1, Tmp);
}

UINT32 HteMemInit(
    MRC_PARAMS *CurrentMrcData,
    UINT8 MemInitFlag,
    UINT8 HaltHteEngineOnError)

/*++

 Routine Description:

 Uses HW HTE engine to initialize or test all memory attached to a given DUNIT.
 If MemInitFlag is 1, this routine writes 0s to all memory locations to initialize
 ECC.
 If MemInitFlag is 0, this routine will send an 5AA55AA5 pattern to all memory
 locations on the RankMask and then read it back.  Then it sends an A55AA55A
 pattern to all memory locations on the RankMask and reads it back.

 Arguments:

 CurrentMrcData: Host struture for all MRC global data.
 MemInitFlag: 0 for memtest, 1 for meminit.
 HaltHteEngineOnError:  Halt the HTE engine on first error observed, or keep
 running to see how many errors are found.

 Returns:
 Errors register showing HTE failures.
 Also prints out which rank failed the HTE test if failure occurs.
 For rank detection to work, the address map must be left in its default
 state.  If MRC changes the address map, this function must be modified
 to change it back to default at the beginning, then restore it at the end.

 --*/
{
  UINT32 Offset;
  UINT8 TestNum;
  UINT8 i;

  //
  // Clear out the error registers at the start of each memory
  // init or memory test run.
  //
  ClearHteErrorRegisters();

  isbW32m(HTE, 0x00020062, 0x00000015);

  for (Offset = 0x80; Offset <= 0x8F; Offset++)
  {
    isbW32m(HTE, Offset, ((Offset & 1) ? 0xA55A : 0x5AA5));
  }

  isbW32m(HTE, 0x00020021, 0x00000000);
#ifdef QUICKSIM
  // Just do 4 cache lines for simulation memtest to save time.
  isbW32m(HTE, 0x00020022, 4-1);
#else
  isbW32m(HTE, 0x00020022, (CurrentMrcData->mem_size >> 6) - 1);
#endif

  isbW32m(HTE, 0x00020063, 0xAAAAAAAA);
  isbW32m(HTE, 0x00020064, 0xCCCCCCCC);
  isbW32m(HTE, 0x00020065, 0xF0F0F0F0);
  isbW32m(HTE, 0x00020066, 0x03000000);

  switch (MemInitFlag)
  {
  case MrcMemInit:
    TestNum = 1; // Only 1 write pass through memory is needed to initialize ECC.
    break;
  case MrcMemTest:
    TestNum = 4; // Write/read then write/read with inverted pattern.
    break;
  default:
    DPF(D_INFO, "Unknown parameter for MemInitFlag: %d\n", MemInitFlag);
    return 0xFFFFFFFF;
    break;
  }

  DPF(D_INFO, "HteMemInit");
  for (i = 0; i < TestNum; i++)
  {
    DPF(D_INFO, ".");

    if (i == 0)
    {
      isbW32m(HTE, 0x00020061, 0x00000000);
      isbW32m(HTE, 0x00020020, 0x00110010);
    }
    else if (i == 1)
    {
      isbW32m(HTE, 0x00020061, 0x00000000);
      isbW32m(HTE, 0x00020020, 0x00010010);
    }
    else if (i == 2)
    {
      isbW32m(HTE, 0x00020061, 0x00010100);
      isbW32m(HTE, 0x00020020, 0x00110010);
    }
    else
    {
      isbW32m(HTE, 0x00020061, 0x00010100);
      isbW32m(HTE, 0x00020020, 0x00010010);
    }

    isbW32m(HTE, 0x00020011, 0x00111000);
    isbW32m(HTE, 0x00020011, 0x00111100);

    WaitForHteComplete();

    //
    // If this is a READ pass, check for errors at the end.
    //
    if ((i % 2) == 1)
    {
      //
      // Return immediately if  error.
      //
      if (CheckHteErrors())
      {
        break;
      }
    }
  }

  DPF(D_INFO, "done\n", i);
  return CheckHteErrors();
}

STATIC UINT16 BasicDataCompareHte(
    MRC_PARAMS *CurrentMrcData,
    UINT32 Address,
    UINT8 FirstRun,
    UINT8 Mode)
/*++

 Routine Description:

 Execute basic single cache line memory write/read/verify test using simple constant
 pattern (different for READ_RAIN and WRITE_TRAIN modes.
 See BasicWriteReadHTE which is external visible wrapper.

 Arguments:

 CurrentMrcData: Host struture for all MRC global data.
 Address: memory adress being tested (must hit specific channel/rank)
 FirstRun: If set then hte registers are configured, otherwise
 it is assumed configuration is done and just re-run the test.
 Mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)

 Returns:
 Returns byte lane failure on each bit (for Quark only bit0 and bit1)

 --*/
{
  UINT32 Pattern;
  UINT32 Offset;

  if (FirstRun)
  {
    isbW32m(HTE, 0x00020020, 0x01B10021);
    isbW32m(HTE, 0x00020021, 0x06000000);
    isbW32m(HTE, 0x00020022, Address >> 6);
    isbW32m(HTE, 0x00020062, 0x00800015);
    isbW32m(HTE, 0x00020063, 0xAAAAAAAA);
    isbW32m(HTE, 0x00020064, 0xCCCCCCCC);
    isbW32m(HTE, 0x00020065, 0xF0F0F0F0);
    isbW32m(HTE, 0x00020061, 0x00030008);

    if (Mode == WRITE_TRAIN)
    {
      Pattern = 0xC33C0000;
    }
    else // READ_TRAIN
    {
      Pattern = 0xAA5555AA;
    }

    for (Offset = 0x80; Offset <= 0x8F; Offset++)
    {
      isbW32m(HTE, Offset, Pattern);
    }
  }

  isbW32m(HTE, 0x000200A1, 0xFFFF1000);

  isbW32m(HTE, 0x00020011, 0x00011000);
  isbW32m(HTE, 0x00020011, 0x00011100);

  WaitForHteComplete();

  //
  // Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for any bytelane errors.
  //
  return ((CheckHteErrors() >> 8) & 0xFF);
}

STATIC UINT16 ReadWriteDataCompareHte(
    MRC_PARAMS *CurrentMrcData,
    UINT32 Address,
    UINT8 LoopCount,
    UINT32 LfsrSeedVictim,
    UINT32 LfsrSeedAggressor,
    UINT8 VictimBit,
    UINT8 FirstRun)
/*++

 Routine Description:

 Examines single cache line memory with write/read/verify test using
 multiple data patterns (victim-aggressor algorithm).
 See WriteStressBitLanesHTE which is external visible wrapper.

 Arguments:

 CurrentMrcData: host struture for all MRC global data.
 Address: memory adress being tested (must hit specific channel/rank)
 LoopCount: number of test iterations
 LfsrSeedXxx: victim aggressor data pattern seed
 VictimBit: should be 0 as auto rotate feature is in use.
 FirstRun: If set then hte registers are configured, otherwise
 it is assumed configuration is done and just re-run the test.

 Returns:
 Returns byte lane failure on each bit (for Quark only bit0 and bit1)

 --*/
{
  UINT32 Offset;
  UINT32 Tmp;

  if (FirstRun)
  {
    isbW32m(HTE, 0x00020020, 0x00910024);
    isbW32m(HTE, 0x00020023, 0x00810024);
    isbW32m(HTE, 0x00020021, 0x06070000);
    isbW32m(HTE, 0x00020024, 0x06070000);
    isbW32m(HTE, 0x00020022, Address >> 6);
    isbW32m(HTE, 0x00020025, Address >> 6);
    isbW32m(HTE, 0x00020062, 0x0000002A);
    isbW32m(HTE, 0x00020063, LfsrSeedVictim);
    isbW32m(HTE, 0x00020064, LfsrSeedAggressor);
    isbW32m(HTE, 0x00020065, LfsrSeedVictim);

    //
    // Write the pattern buffers to select the victim bit. Start with bit0.
    //
    for (Offset = 0x80; Offset <= 0x8F; Offset++)
    {
      if ((Offset % 8) == VictimBit)
      {
        isbW32m(HTE, Offset, 0x55555555);
      }
      else
      {
        isbW32m(HTE, Offset, 0xCCCCCCCC);
      }
    }

    isbW32m(HTE, 0x00020061, 0x00000000);
    isbW32m(HTE, 0x00020066, 0x03440000);
    isbW32m(HTE, 0x000200A1, 0xFFFF1000);
  }

  Tmp = 0x10001000 | (LoopCount << 16);
  isbW32m(HTE, 0x00020011, Tmp);
  isbW32m(HTE, 0x00020011, Tmp | BIT8);

  WaitForHteComplete();

  return (CheckHteErrors() >> 8) & 0xFF;
}

UINT16 BasicWriteReadHTE(
    MRC_PARAMS *CurrentMrcData,
    UINT32 Address,
    UINT8 FirstRun,
    UINT8 Mode)
/*++

 Routine Description:

 Execute basic single cache line memory write/read/verify test using simple constant
 pattern (different for READ_RAIN and WRITE_TRAIN modes.

 Arguments:

 CurrentMrcData: Host struture for all MRC global data.
 Address: memory adress being tested (must hit specific channel/rank)
 FirstRun: If set then hte registers are configured, otherwise
 it is assumed configuration is done and just re-run the test.
 Mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)

 Returns:
 Returns byte lane failure on each bit (for Quark only bit0 and bit1)

 --*/
{
  UINT16 ByteLaneErrors;

  ENTERFN();

  //
  // Enable all error reporting in preparation for HTE test.
  //
  EnableAllHteErrors(0xFF);
  ClearHteErrorRegisters();

  ByteLaneErrors = BasicDataCompareHte(CurrentMrcData, Address, FirstRun,
      Mode);

  LEAVEFN();
  return ByteLaneErrors;
}

UINT16 WriteStressBitLanesHTE(
    MRC_PARAMS *CurrentMrcData,
    UINT32 Address,
    UINT8 FirstRun)
/*++

 Routine Description:

 Examines single cache line memory with write/read/verify test using
 multiple data patterns (victim-aggressor algorithm).

 Arguments:

 CurrentMrcData: host struture for all MRC global data.
 Address: memory adress being tested (must hit specific channel/rank)
 FirstRun: If set then hte registers are configured, otherwise
 it is assumed configuration is done and just re-run the test.

 Returns:
 Returns byte lane failure on each bit (for Quark only bit0 and bit1)

 --*/
{
  UINT16 ByteLaneErrors;
  UINT8 VictimBit = 0;

  ENTERFN();

  //
  // Enable all error reporting in preparation for HTE test.
  //
  EnableAllHteErrors(0xFF);
  ClearHteErrorRegisters();

  //
  // Loop through each bit in the bytelane.  Each pass creates a victim bit
  // while keeping all other bits the same - as aggressors.
  // AVN HTE adds an auto-rotate feature which allows us to program the entire victim/aggressor
  // sequence in 1 step. The victim bit rotates on each pass so no need to have software implement
  // a victim bit loop like on VLV.
  //
  ByteLaneErrors = ReadWriteDataCompareHte(CurrentMrcData, Address,
      HTE_LOOP_CNT, HTE_LFSR_VICTIM_SEED, HTE_LFSR_AGRESSOR_SEED, VictimBit,
      FirstRun);

  LEAVEFN();
  return ByteLaneErrors;
}

VOID HteMemOp(
    UINT32 Address,
    UINT8 FirstRun,
    UINT8 IsWrite)
/*++

 Routine Description:

 Execute basic single cache line memory write or read.
 This is just for receive enable / fine write levelling purpose.

 Arguments:

 CurrentMrcData: Host structure for all MRC global data.
 Address: memory address used (must hit specific channel/rank)
 FirstRun: If set then hte registers are configured, otherwise
 it is assumed configuration is done and just re-run the test.
 IsWrite: When non-zero memory write operation executed, otherwise read

 Returns:
 None

 --*/
{
  UINT32 Offset;
  UINT32 Tmp;

  EnableAllHteErrors(0xFF);
  ClearHteErrorRegisters();

  if (FirstRun)
  {
    Tmp = IsWrite ? 0x01110021 : 0x01010021;
    isbW32m(HTE, 0x00020020, Tmp);

    isbW32m(HTE, 0x00020021, 0x06000000);
    isbW32m(HTE, 0x00020022, Address >> 6);
    isbW32m(HTE, 0x00020062, 0x00800015);
    isbW32m(HTE, 0x00020063, 0xAAAAAAAA);
    isbW32m(HTE, 0x00020064, 0xCCCCCCCC);
    isbW32m(HTE, 0x00020065, 0xF0F0F0F0);
    isbW32m(HTE, 0x00020061, 0x00030008);

    for (Offset = 0x80; Offset <= 0x8F; Offset++)
    {
      isbW32m(HTE, Offset, 0xC33C0000);
    }
  }

  isbW32m(HTE, 0x000200A1, 0xFFFF1000);
  isbW32m(HTE, 0x00020011, 0x00011000);
  isbW32m(HTE, 0x00020011, 0x00011100);

  WaitForHteComplete();
}