summaryrefslogtreecommitdiff
path: root/src/vendorcode/amd/cimx/rd890/nbHtInit.c
blob: 687219a35941c5469fc438cd027cc3fac8205d2f (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
/**
 * @file
 *
 * HT Initialization
 *
 *
 *
 * @xrefitem bom "File Content Label" "Release Content"
 * @e project:      CIMx-NB
 * @e sub-project:
 * @e \$Revision:$   @e \$Date:$
 *
 */
/*****************************************************************************
 *
 * Copyright (C) 2012 Advanced Micro Devices, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
 *       its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 ***************************************************************************/
/*----------------------------------------------------------------------------------------
 *                             M O D U L E S    U S E D
 *----------------------------------------------------------------------------------------
 */

#include "NbPlatform.h"
#include "amdDebugOutLib.h"

/*----------------------------------------------------------------------------------------
 *                   D E F I N I T I O N S    A N D    M A C R O S
 *----------------------------------------------------------------------------------------
 */


/*----------------------------------------------------------------------------------------
 *                  T Y P E D E F S     A N D     S T R U C T U  R E S
 *----------------------------------------------------------------------------------------
 */
#define LINK_BUFFERS_IFCM  ((1 << 31) + (1 << 25) + (8 << 20) + (1 << 18) + (1 << 8) + (6 << 5) + 0xF)
#define LINK_BUFFERS_NFCM  ((1 << 31) + (1 << 25) + (8 << 20) + (1 << 18) + (1 << 8) + (6 << 5) + 0x11)
#define SUBLINK_BUFFERS_IFCM  ((1 << 31) + (1 << 25) + (8 << 20) + (1 << 18) + (1 << 8) + (6 << 5) + 0xF)
#define SUBLINK_BUFFERS_NFCM  ((1 << 31) + (1 << 25) + (8 << 20) + (1 << 18) + (1 << 8) + (6 << 5) + 0x11)

typedef VOID DUMMY_CALL (VOID);

/*----------------------------------------------------------------------------------------
 *           P R O T O T Y P E S     O F     L O C A L     F U  N C T I O N S
 *----------------------------------------------------------------------------------------
 */

VOID
NbInitRasParityMacro (
  IN      AMD_NB_CONFIG *NbConfigPtr
  );

/*----------------------------------------------------------------------------------------
 *                          E X P O R T E D    F U N C T I O N S
 *----------------------------------------------------------------------------------------
 */

UINT8 IBIASCodeTable[] = {
//0.2G        0.5G        0.6G
  0x44, 0x00, 0x44, 0x00, 0xb6,
//0.8G  1.0G  1.2G  1.4G  1.6G
  0x44, 0x96, 0xb6, 0x23, 0x44,
//1.8G  2.0G  2.2G  2.4G  2.6G
  0x64, 0x96, 0xA6, 0xb6, 0xc6
};



/*----------------------------------------------------------------------------------------*/
/**
 * NB Init at early post.
 *
 *
 *
 * @param[in] pConfig   Northbridge configuration structure pointer.
 *
 */

AGESA_STATUS
HtLibEarlyInit (
  IN OUT   AMD_NB_CONFIG *pConfig
  )
{
  AGESA_STATUS            Status;
  UINT32                  Value;
  PCI_ADDR                ClkPciAddress;
  PCI_ADDR                CpuPciAddress;
  PCI_ADDR                LinkPciAddress;
  HT_CONFIG               *pHtConfig;
  UINT8                   CpuHtSpeed;
  UINT8                   NbHtSpeed;
  HT_INACTIVE_LANE_STATE  InLnSt;
  BOOLEAN                 IsIfcmEnabled;

  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "[NBHT]HtLibEarlyInit Enter\n"));
  pHtConfig = GET_HT_CONFIG_PTR (pConfig);
  Status = AGESA_SUCCESS;
  ClkPciAddress = pConfig->NbPciAddress;
  ClkPciAddress.Address.Device = 1;
  CpuPciAddress.AddressValue = MAKE_SBDFO (0, 0, pConfig->NbHtPath.NodeID + 0x18, 0,  0);
  LinkPciAddress.AddressValue = MAKE_SBDFO (0, 0, pConfig->NbHtPath.NodeID + 0x18, ((pConfig->NbHtPath.LinkID & 0xF0) > 0x10)?4:0,  0);
  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "    Node %d Link %d PciAddress %x\n", pConfig->NbHtPath.NodeID, pConfig->NbHtPath.LinkID, LinkPciAddress.AddressValue));
  LibNbEnableClkConfig (pConfig);
//Get Ht Speed  Info
  LibNbPciRead (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x89), AccessWidth8, &CpuHtSpeed, pConfig);
  LibNbPciRead (pConfig->NbPciAddress.AddressValue | NB_PCI_REGD1 , AccessWidth8, &NbHtSpeed, pConfig);
  NbHtSpeed &= 0xf;
  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "    Ht speed Cpu %x Nb %x\n", CpuHtSpeed, NbHtSpeed));
//Set IBIAS code
  LibNbPciRMW (ClkPciAddress.AddressValue | NB_CLK_REGD8, AccessWidth16, (UINT32)~(0x3ff), ((UINT8*)FIX_PTR_ADDR (&IBIASCodeTable[0], NULL))[(pHtConfig->HtReferenceClock / 200)*CpuHtSpeed], pConfig);
  if (CpuHtSpeed > HT_FREQUENCY_1000M) {
    UINT8 T0Time;
    UINT8 ForceFullT0;
//Enable Protocol checker
    LibNbPciIndexRMW (pConfig->NbPciAddress.AddressValue | NB_HTIU_INDEX, NB_HTIU_REG1E, AccessWidth32, 0, 0x7FFFFFFC, pConfig);
//Set NB Transmitter Deemphasis
    if ((pHtConfig->NbTransmitterDeemphasis & 0x80) == 0) {
      LibHtSetNbTransmitterDeemphasis (pHtConfig->NbTransmitterDeemphasis, pConfig);
    }
    LibNbPciRead (CpuPciAddress.AddressValue | 0x16C, AccessWidth32, &Value, pConfig);
    T0Time = (UINT8) (Value & 0x3F);
    ForceFullT0 = (UINT8) ((Value >> 13) & 0x7);
//Enable LS State and set T0Time
    //T0Time = 0x14; //2us
    if (pHtConfig->LSx <  HtLinkStateSkipInit) {
      if (pHtConfig->LSx == HtLinkStateSameAsCpu) {
        LibNbPciRead (
          CpuPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 4 + HT_PATH_SUBLINK_ID (pConfig->NbHtPath) * 0x10 + 0x170),
          AccessWidth32,
          &Value,
          pConfig
          );
        if ((Value & BIT8) != 0) {
          pHtConfig->LSx = HtLinkStateLS2;
        } else {
          pHtConfig->LSx = HtLinkStateLS1;
        }
      } else {
        if (pHtConfig->LSx >=  HtLinkStateLS2) {
          T0Time = 0x26; //12us
          ForceFullT0 = 0x6;
        } else {
          T0Time = 0x14; //2us
          ForceFullT0 = 0x0;
        }
      }
      if (CpuHtSpeed == NbHtSpeed) {
        LibHtEnableLxState (pHtConfig->LSx, pConfig);
      }
    }
//Set up InLnSt
    //Match CPU InLnSt except for HT3 LS1
    LibNbPciRead (
      CpuPciAddress.AddressValue | 0x16C,
      AccessWidth32,
      &Value,
      pConfig
      );
    InLnSt = (Value >> 6) & 0x3;

    // Do not enable HT3 LS1 with InLnSt == 0x1 (PHY_OFF) as per errata 9
    if (pHtConfig->LSx == HtLinkStateLS1) {
      if (InLnSt == InactiveLaneStateSameAsPhyOff) {
        InLnSt = InactiveLaneStateCadCtrlDrivelToLogic0;
      }
    }
    LibNbPciRMW (
      pConfig->NbPciAddress.AddressValue | NB_PCI_REGA0,
      AccessWidth8,
      0x00,
      T0Time | (InLnSt << 6),
      pConfig
      );
    LibNbPciRMW (
      CpuPciAddress.AddressValue | 0x16C,
      AccessWidth16,
      (UINT32)(~((0x7 << 13) + 0x3f)),
      T0Time | (ForceFullT0 << 13),
      pConfig
      );
  // Disable command throtling
    LibNbPciRMW (pConfig->NbPciAddress.AddressValue | (NB_PCI_REGAC + 1), AccessWidth8, (UINT32)~BIT6, BIT6, pConfig);
    LibNbPciRMW (CpuPciAddress.AddressValue | 0x168, AccessWidth16, (UINT32)~BIT10, BIT10, pConfig);
  // Enables strict TM4 detection
    LibNbPciIndexRMW (pConfig->NbPciAddress.AddressValue | NB_HTIU_INDEX, NB_HTIU_REG15, AccessWidth32, (UINT32)~BIT22, BIT22, pConfig);
  } else {
  //Set Link Tristate
    if (pHtConfig->HtLinkTriState < HtLinkTriStateSkip) {
      if (pHtConfig->HtLinkTriState == HtLinkTriStateSameAsCpu) {
        LibNbPciRead (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x84), AccessWidth16, &Value, pConfig);
        if ((Value & BIT13) != 0) {
          pHtConfig->HtLinkTriState = HtLinkTriStateCadCtl;
          LibNbPciRead (
            CpuPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 4 + HT_PATH_SUBLINK_ID (pConfig->NbHtPath) * 0x10 + 0x170),
            AccessWidth16,
            &Value,
            pConfig
            );
          if ((Value & BIT8) != 0) {
            pHtConfig->HtLinkTriState = HtLinkTriStateCadCtlClk;
          }
        }
      }
      if (pHtConfig->HtLinkTriState >= HtLinkTriStateCadCtl) {
        UINT16 TriStateValue;
        TriStateValue = 0;
        LibNbPciRMW (pConfig->NbPciAddress.AddressValue | NB_PCI_REGC8, AccessWidth16 , (UINT32)~BIT13, BIT13, pConfig);
        LibNbPciRMW (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x84), AccessWidth16, (UINT32)~(BIT13), BIT13, pConfig);
        if (pHtConfig->HtLinkTriState == HtLinkTriStateCadCtlClk) {
          TriStateValue = BIT8;
        }
        LibNbPciRMW (pConfig->NbPciAddress.AddressValue | NB_PCI_REGAC, AccessWidth16, (UINT32)~BIT8, TriStateValue, pConfig);
        if (LibNbGetCpuFamily () != 0x0) {
          LibNbPciRMW (
            CpuPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 4 + HT_PATH_SUBLINK_ID (pConfig->NbHtPath) * 0x10 + 0x170),
            AccessWidth16,
            (UINT32)~(BIT8),
            TriStateValue,
            pConfig
            );
        }
      }
    }
    LibNbPciRMW (
      pConfig->NbPciAddress.AddressValue | NB_PCI_REGA0,
      AccessWidth8,
      0x3f,
      InactiveLaneStateSameAsPhyOff << 6,
      pConfig
      );
  }
  //Enable 64bit address mode

  if ((pHtConfig->HtExtendedAddressSupport & 0x80) == 0) {
    LibNbPciRead (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x85), AccessWidth8, &Value, pConfig);
    if (pHtConfig->HtExtendedAddressSupport == HtExtAddressingSameAsCpu) {
      Value &= BIT7;
    } else {
      Value = (pHtConfig->HtExtendedAddressSupport == HtExtAddressingEnable)? BIT7 : 0;
    }
    LibNbPciRMW (((pConfig->NbPciAddress.AddressValue) | (NB_PCI_REGC8 + 1)), AccessWidth8, 0x7C, Value, pConfig);
    LibNbPciRMW (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x85), AccessWidth8, 0x7F, Value, pConfig);
  }

  // Check if IFCM enabled in CPU
  LibNbPciRead (LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x85), AccessWidth8, &Value, pConfig);
  IsIfcmEnabled = (Value & BIT4) ? TRUE:FALSE;
  if (IsIfcmEnabled) {
    // Enable Isoc in chipset
    LibNbPciRMW (((pConfig->NbPciAddress.AddressValue) | (NB_PCI_REGC8 + 1)), AccessWidth8, 0xFC, BIT4, pConfig);
  }

  if (pHtConfig->LinkBufferOptimization == ON) {
    BOOLEAN IsConnectedToSublink;
    IsConnectedToSublink = (pConfig->NbHtPath.LinkID & 0xF0) > 0 ? TRUE:FALSE;
    if (IsConnectedToSublink) {
      Value = IsIfcmEnabled ? SUBLINK_BUFFERS_IFCM : SUBLINK_BUFFERS_NFCM;
    } else {
      Value = IsIfcmEnabled ? LINK_BUFFERS_IFCM : LINK_BUFFERS_NFCM;
    }
    LibNbPciRMW (
      LinkPciAddress.AddressValue | (HT_PATH_LINK_ID (pConfig->NbHtPath) * 32 + 0x90),
      AccessWidth32,
      0x0,
      Value,
      pConfig
      );
  }
  NbInitRasParityMacro (pConfig);
  LibNbDisableClkConfig (pConfig);
  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "[NBHT]HtLibEarlyInit Exit [0x%x]\n", Status));
  return  Status;
}

/*----------------------------------------------------------------------------------------*/
/**
 * Set NB transmitter deemphasis level.
 *
 *
 * @param[in] NbDeemphasisLevel  NB Deemphasis level See HT_CONFIG::NbTransmitterDeemphasis
 * @param[in] pConfig           Northbridge configuration structure pointer.
 *
 */
VOID
LibHtSetNbTransmitterDeemphasis (
  IN      UINT8         NbDeemphasisLevel,
  IN      AMD_NB_CONFIG *pConfig
  )
{
  LibNbPciRMW (pConfig->NbPciAddress.AddressValue | NB_PCI_REGA7, AccessWidth8, (UINT32)~0x07, NbDeemphasisLevel + BIT7, pConfig);
}

/*----------------------------------------------------------------------------------------*/
/**
 * Enable LSx state
 *
 *
 *
 * @param[in] LSx     LS State to enable. See HT_CONFIG::LSx
 * @param[in] pConfig   Northbridge configuration structure pointer.
 */
VOID
LibHtEnableLxState (
  IN      UINT8         LSx,
  IN      AMD_NB_CONFIG *pConfig
  )
{
  UINT32  Value;
  UINT32  NbLSx;
  UINT32  CpuLSx;

  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "    Enable HT LS%d\n", LSx));
  switch (LSx) {
  case 0:
    Value = LS0;
    break;
  case 1:
    Value = LS1;
    break;
  case 2:
    Value = LS2;
    break;
  case 3:
    Value = LS3;
    break;
  default:
    Value = 0;
    CIMX_ASSERT (FALSE);
    return;
  }
  NbLSx = (Value << 7);
  CpuLSx = (LSx >= 2)?BIT8:0;
  LibNbPciRMW (pConfig->NbPciAddress.AddressValue | NB_PCI_REGAC, AccessWidth16, (UINT32)~(BIT7 + BIT8), NbLSx, pConfig);
  LibNbPciRMW (
    MAKE_SBDFO (0, 0, pConfig->NbHtPath.NodeID + 0x18, 0, HT_PATH_LINK_ID (pConfig->NbHtPath) * 4 + HT_PATH_SUBLINK_ID (pConfig->NbHtPath) * 0x10 + 0x170),
    AccessWidth32,
    (UINT32)~BIT8,
    CpuLSx,
    pConfig
    );
}


/*----------------------------------------------------------------------------------------*/
/**
 * NB validate HY Input parameters
 *
 *
 *
 * @param[in] pConfig           Northbridge configuration structure pointer.
 *
 */

AGESA_STATUS
HtLibInitValidateInput (
  IN OUT   AMD_NB_CONFIG *pConfig
  )
{
  AGESA_STATUS  Status;
  HT_CONFIG     *pHtConfig;
  NB_INFO       NbInfo;

  Status = AGESA_SUCCESS;
  pHtConfig = GET_HT_CONFIG_PTR (pConfig);
  NbInfo = LibNbGetRevisionInfo (pConfig);
  if (pHtConfig == NULL || NbInfo.Type == NB_UNKNOWN) {
    return  AGESA_FATAL;
  }
  if (pHtConfig->sHeader.InitializerID != INITIALIZED_BY_INITIALIZER) {
    Status = HtLibInitializer (pConfig);
  }
  return  Status;
}

UINT8 SmuWaBasePeriod[] = {
  0x1F,   //HT 200Mhz
  0x00,
  0x1F,   //HT 400Mhz
  0x00,
  0x17,   //HT 600Mhz
  0x1F,   //HT 800Mhz
  0x27,   //HT 1000Mhz
  0x2E,   //HT 1200Mhz
  0x36,   //HT 1400Mhz
  0x3E,   //HT 1600Mhz
  0x46,   //HT 1800Mhz
  0x4E,   //HT 2000Mhz
  0x55,   //HT 2200Mhz
  0x5D,   //HT 2400Mhz
  0x65    //HT 2600Mhz
};

UINT8 SmuWaBaseDelay[] = {
  0x3,   //HT 200Mhz
  0x0,
  0x3,   //HT 400Mhz
  0x0,
  0x2,   //HT 600Mhz
  0x3,   //HT 800Mhz
  0x3,   //HT 1000Mhz
  0x4,   //HT 1200Mhz
  0x5,   //HT 1400Mhz
  0x6,   //HT 1600Mhz
  0x7,   //HT 1800Mhz
  0x7,   //HT 2000Mhz
  0x8,   //HT 2200Mhz
  0x9,   //HT 2400Mhz
  0xA    //HT 2600Mhz
};

UINT8 SmuWaPeriod10us[] = {
  120,
  100,
  90,
  80
};

UINT8 SmuWaDelay1us[] = {
  0x0,   //HT 200Mhz
  0x0,
  0x0,   //HT 400Mhz
  0x0,
  0x0,   //HT 600Mhz
  0x0,   //HT 800Mhz
  0x0,   //HT 1000Mhz
  0x0,   //HT 1200Mhz
  0x2,   //HT 1400Mhz
  0x2,   //HT 1600Mhz
  0x2,   //HT 1800Mhz
  0x3,   //HT 2000Mhz
  0x3,   //HT 2200Mhz
  0x4,   //HT 2400Mhz
  0x4    //HT 2600Mhz
};

/*----------------------------------------------------------------------------------------*/
/**
 * Get SMU wa data
 *
 *
 *
 * @param[in] pConfig           Northbridge configuration structure pointer.
 * @retval                      SMU wa data
 */

UINT32
LibHtGetSmuWaData (
  IN       AMD_NB_CONFIG *pConfig
  )
{
  UINT8                 NorthbridgeId;
  UINT8                 NorthbridgeIndex;
  UINT8                 NbHtSpeed;
  UINT16                SmuWaPeriod;
  UINT16                SmuWaDelay;
  AMD_NB_CONFIG_BLOCK   *ConfigPtr;
  NorthbridgeIndex = 0;
  ConfigPtr = GET_BLOCK_CONFIG_PTR (pConfig);
  for (NorthbridgeId = 0; NorthbridgeId <= ConfigPtr->NumberOfNorthbridges; NorthbridgeId++) {
    AMD_NB_CONFIG *NbConfigPtr = &ConfigPtr->Northbridges[NorthbridgeId];
    if (LibNbIsDevicePresent (NbConfigPtr->NbPciAddress, NbConfigPtr)) {
      if (pConfig == NbConfigPtr) {
        LibNbPciRead (pConfig->NbPciAddress.AddressValue | NB_PCI_REGD1 , AccessWidth8, &NbHtSpeed, pConfig);
        NbHtSpeed &= 0xf;
        SmuWaPeriod = SmuWaPeriod10us [NorthbridgeIndex] * SmuWaBasePeriod [NbHtSpeed];
        SmuWaDelay = SmuWaDelay1us [NbHtSpeed] * SmuWaBaseDelay [NbHtSpeed];
        return ((SmuWaPeriod & 0xFF) << 8) | ((SmuWaPeriod & 0xFF00) >> 8) | ((SmuWaDelay & 0xFF) << 24) | ((SmuWaDelay & 0xFF00) << 8);
      }
      NorthbridgeIndex++;
    }
  }
  return 0;
}


/*----------------------------------------------------------------------------------------*/
/**
 * Init RAS macro
 *
 *
 *
 *
 *  @param[in] NbConfigPtr  Northbridge configuration structure pointer.
 */
VOID
NbInitRasParityMacro (
  IN      AMD_NB_CONFIG *NbConfigPtr
  )
{
  PCI_ADDR    CpuPciAddress;
  UINT32      SaveBase;
  UINT32      SaveLimit;
  UINT32      Value;
  UINT32      Base;
  UINT32      Limit;
  UINT32      Index;
  UINT64      SaveTom;
  UINT64      Value64;
  DUMMY_CALL  *RetAddr;
  UINT8       Node;
  CpuPciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1,  0);
//Set TOM
  LibAmdMsrRead (0xC001001a, &SaveTom, (AMD_CONFIG_PARAMS *)NbConfigPtr);
  Value64 = 0x40000000;
  LibAmdMsrWrite (0xC001001a, &Value64, (AMD_CONFIG_PARAMS *)NbConfigPtr);
//Set mmio
  LibNbPciRead (CpuPciAddress.AddressValue | 0x80, AccessWidth32, &SaveBase, NbConfigPtr);
  LibNbPciRead (CpuPciAddress.AddressValue | 0x84, AccessWidth32, &SaveLimit, NbConfigPtr);
  Limit = ((0x50000000 - 1) >> 8) & (~ 0xFF);
  Limit |= NbConfigPtr->NbHtPath.NodeID | (HT_PATH_LINK_ID (NbConfigPtr->NbHtPath) << 4) | (HT_PATH_SUBLINK_ID (NbConfigPtr->NbHtPath) << 6);
  Base = ((0x40000000 >> 8) & (~ 0xFF)) | 0x3;
  for (Node = 0; Node < 8; Node++) {
    CpuPciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18 + Node, 1,  0);
    if (LibNbIsDevicePresent (CpuPciAddress, NbConfigPtr)) {
      LibNbPciWrite (CpuPciAddress.AddressValue | 0x84, AccessWidth32, &Limit, NbConfigPtr);
      LibNbPciWrite (CpuPciAddress.AddressValue | 0x80, AccessWidth32, &Base, NbConfigPtr);
    } else {
      break;
    }
  }
//set Scan
  LibNbPciIndexRMW (NbConfigPtr->NbPciAddress.AddressValue | NB_HTIU_INDEX, NB_HTIU_REG06, AccessWidth32, (UINT32)~BIT27, BIT27, NbConfigPtr);
  RetAddr = (DUMMY_CALL* ) (UINTN) 0x40000000;
  *((UINT8*) (UINTN) RetAddr) = 0xC3;
  for (Index = 0; Index < 64; Index++) {
    RetAddr ();
    RetAddr = (DUMMY_CALL*) (UINTN) ((UINT8*) (UINTN) RetAddr + 64);
  }
//Reset scan
  LibNbPciIndexRMW (NbConfigPtr->NbPciAddress.AddressValue | NB_HTIU_INDEX, NB_HTIU_REG06, AccessWidth32, (UINT32)~BIT27, 0x0, NbConfigPtr);
  Value = 0;
// Restore MMIO Map
  for (Node = 0; Node < 8; Node++) {
    CpuPciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18 + Node, 1,  0);
    if (LibNbIsDevicePresent (CpuPciAddress, NbConfigPtr)) {
      LibNbPciWrite (CpuPciAddress.AddressValue | 0x80, AccessWidth32, &Value, NbConfigPtr);
      LibNbPciWrite (CpuPciAddress.AddressValue | 0x84, AccessWidth32, &SaveLimit, NbConfigPtr);
      LibNbPciWrite (CpuPciAddress.AddressValue | 0x80, AccessWidth32, &SaveBase, NbConfigPtr);
    } else {
      break;
    }
  }
// Restore TOM
  LibAmdMsrWrite (0xC001001a, &SaveTom, (AMD_CONFIG_PARAMS *)NbConfigPtr);
}

/*----------------------------------------------------------------------------------------*/
/**
 * AMD structures initializer for all NB.
 *
 *
 *
 * @param[in] ConfigPtr   Northbridges configuration block pointer.
 *
 */

AGESA_STATUS
AmdHtInitializer (
  IN OUT   AMD_NB_CONFIG_BLOCK *ConfigPtr
  )
{
  AGESA_STATUS  Status;
  Status = LibNbApiCall (HtLibInitializer, ConfigPtr);
  return Status;
}
/*----------------------------------------------------------------------------------------*/
/**
 * HT config structure initializer
 *
 *
 *
 * @param[in] pConfig           Northbridge configuration structure pointer.
 *
 */

AGESA_STATUS
HtLibInitializer (
  IN OUT   AMD_NB_CONFIG *pConfig
  )
{
  HT_CONFIG *pHtConfig;

  pHtConfig = GET_HT_CONFIG_PTR (pConfig);
  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "[NBHT]HtLibInitializer Enter\n"));
  if (pHtConfig == NULL) {
    return  AGESA_WARNING;
  }
  if (pHtConfig->sHeader.InitializerID == INITIALIZED_BY_INITIALIZER) {
    return  AGESA_SUCCESS;
  }
  LibAmdMemFill (pHtConfig, 0, sizeof (HT_CONFIG), (AMD_CONFIG_PARAMS *)&(pHtConfig->sHeader));
  pHtConfig->sHeader.InitializerID = INITIALIZED_BY_INITIALIZER;
  pHtConfig->HtExtendedAddressSupport = HtExtAddressingSameAsCpu;
  pHtConfig->HtLinkTriState = HtLinkTriStateSameAsCpu;
  pHtConfig->HtReferenceClock = 200;
// Select LS State
  pHtConfig->LSx = HtLinkStateSameAsCpu;
  pHtConfig->LinkBufferOptimization = OFF;
  CIMX_TRACE ((TRACE_DATA (GET_BLOCK_CONFIG_PTR (pConfig), CIMX_NBHT_TRACE), "[NBHT]HtLibInitializer Exit\n"));
  return AGESA_SUCCESS;
}