summaryrefslogtreecommitdiffstats
path: root/src/vendorcode/intel/fsp/rangeley/srx/fspapi.c
blob: 78978f576efa7cd43a45f8c6134db4cf54c7d370 (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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2007-2008 coresystems GmbH
 * Copyright (C) 2013, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

// __PRE_RAM__ means: use "unsigned" for device, not a struct.

#include <stdint.h>
#include <string.h>
#include <arch/io.h>
#include <arch/romcc_io.h>
#include <device/pci_def.h>
#include <device/pnp_def.h>
#include <cpu/x86/lapic.h>
#include <cpu/x86/stack.h>
#include <pc80/mc146818rtc.h>
#include <console/console.h>
#include <spi.h>
#include <spi_flash.h>
#include <cbmem.h>
#include <ip_checksum.h>

#include "fsptypes.h"
#include "fspfv.h"
#include "fspffs.h"
#include "fsphob.h"
#include "fspvpd.h"
#include "fspapi.h"
#include "fspplatform.h"
#include "fspinfoheader.h"
#include "fspbootmode.h"
#include "northbridge/intel/rangeley/northbridge.h"

void find_fsp_header(void);
void early_init(FSP_INFO_HEADER *fsp_info);
void FspNotifyPhase(uint32_t Phase);
void UpdateFspUpdConfigs (
  UPD_DATA_REGION        *FspUpdRgn
);

//
// Spd Data table for Memory Down
//
#ifdef __PRE_RAM__
#if CONFIG_MEMORY_DOWN_ENABLE
/*
  * MEM_DOWN_DIMM_CONFIG
  *   MemoryDownDimmPopulation - 0: Empty 1: Populated.
  *   MemoryDownDimmSpdData - structure MEM_DOWN_DIMM_SPD_DATA for spd data.
 */
  /* Apacer 2GB PC3-12800 DDR3-1600MHz ECC Unbuffered CL11 240-Pin DIMM Single Rank Memory Module Mfr P/N 78.A1GG3.AF20C  */
static const MEM_DOWN_DIMM_CONFIG OemMemoryDownDimmConfig = {
0x01, // MemoryDownDimmPopulation
{  // MemoryDownDimmSpdData
  0xB,    //Byte   2  Dram Device Type
  0x2,    //Byte   3  Module type (3:0)
  0x3,    //Byte   4  Density and Banks
  0x19,   //Byte   5  Addressing
  0x2,    //Byte   6  Nominal Voltage, VDD
  0x1,    //Byte   7  Module Organization
  0xB,    //Byte   8  Memory Bus Width
  0x1,    //Byte  10  Medium Timebase (MTB) Dividend
  0x8,    //Byte  11  Medium Timebase (MTB) Divisor
  0xA,    //Byte  12  Minimum cycle time (tCKmin)
  0xFC,   //Byte  14  CAS latency supported, low byte
  0,      //Byte  15  CAS latency supported, high byte
  0x69,   //Byte  16  Minimum CAS latency time (tAA)
  0x78,   //Byte  17  Minimum write recovery time (tWR)
  0x69,   //Byte  18  Minimum RAS to CAS time (tRCD)
  0x30,   //Byte  19  Minimum RA to RA time (tRRD)
  0x69,   //Byte  20  Minimum precharge time (tRP)
  0x11,   //Byte  21  Upper nibbles for tRAS (7:4), tRC (3:0)
  0x18,   //Byte  22  Minimum active to precharge (tRAS)
  0x81,   //Byte  23  Minimum active to active/refresh (tRC)
  0,      //Byte  24  Minimum refresh recovery (tRFC), low byte
  0x5,    //Byte  25  Minimum refresh recovery (tRFC), high byte
  0x3C,   //Byte  26  Minimum internal wr to rd cmd (tWTR)
  0x3C,   //Byte  27  Minimum internal rd to pc cmd (tRTP)
  0,      //Byte  28  Upper Nibble for tFAW
  0xF0,   //Byte  29  Minimum Four Activate Window Delay Time (tFAWmin), Least Significant Byte
  0x80,   //Byte  32
  0,      //Byte  33
  0,      //Byte  34
  0,      //Byte  35
  0,      //Byte  41
  0x3,    //Byte  62
  0,      //Byte  63
  0x1,    //Byte 117
  0x7A,   //Byte 118
  0,      //Byte 119
  0x13,   //Byte 120
  0x15,   //Byte 121
  0x2,    //Byte 122
  0x1,    //Byte 123
  0x31,   //Byte 124
  0x52,   //Byte 125
  0,      //Byte 148
  0,      //Byte 149
}
};

  /* Dummy structure  */
static const MEM_DOWN_DIMM_CONFIG DummyMemoryDownDimmConfig = {
0x00, // MemoryDownDimmPopulation
{  // MemoryDownDimmSpdData
  0,    //Byte   2  Dram Device Type
  0,    //Byte   3  Module type (3:0)
  0,    //Byte   4  Density and Banks
  0,   //Byte   5  Addressing
  0,    //Byte   6  Nominal Voltage, VDD
  0,    //Byte   7  Module Organization
  0,    //Byte   8  Memory Bus Width
  0,    //Byte  10  Medium Timebase (MTB) Dividend
  0,    //Byte  11  Medium Timebase (MTB) Divisor
  0,    //Byte  12  Minimum cycle time (tCKmin)
  0,   //Byte  14  CAS latency supported, low byte
  0,      //Byte  15  CAS latency supported, high byte
  0,   //Byte  16  Minimum CAS latency time (tAA)
  0,   //Byte  17  Minimum write recovery time (tWR)
  0,   //Byte  18  Minimum RAS to CAS time (tRCD)
  0,   //Byte  19  Minimum RA to RA time (tRRD)
  0,   //Byte  20  Minimum precharge time (tRP)
  0,   //Byte  21  Upper nibbles for tRAS (7:4), tRC (3:0)
  0,   //Byte  22  Minimum active to precharge (tRAS)
  0,   //Byte  23  Minimum active to active/refresh (tRC)
  0,      //Byte  24  Minimum refresh recovery (tRFC), low byte
  0,    //Byte  25  Minimum refresh recovery (tRFC), high byte
  0,   //Byte  26  Minimum internal wr to rd cmd (tWTR)
  0,   //Byte  27  Minimum internal rd to pc cmd (tRTP)
  0,      //Byte  28  Upper Nibble for tFAW
  0,   //Byte  29  Minimum Four Activate Window Delay Time (tFAWmin), Least Significant Byte
  0,   //Byte  32
  0,      //Byte  33
  0,      //Byte  34
  0,      //Byte  35
  0,      //Byte  41
  0,    //Byte  62
  0,      //Byte  63
  0,    //Byte 117
  0,   //Byte 118
  0,    //Byte 119
  0,   //Byte 120
  0,   //Byte 121
  0,    //Byte 122
  0,    //Byte 123
  0,   //Byte 124
  0,   //Byte 125
  0,      //Byte 148
  0,      //Byte 149
}
};
#endif
#endif

//
// FSP_INFO_HEADER GUID
//
#define FSP_INFO_HEADER_GUID \
  { \
  0x912740BE, 0x2284, 0x4734, {0xB9, 0x71, 0x84, 0xB0, 0x27, 0x35, 0x3F, 0x0C} \
  }

//
// FSP_NON_VOLATILE_STORAGE_HOB_GUID
//
#define FSP_NON_VOLATILE_STORAGE_HOB_GUID \
  { \
  0x721acf02, 0x4d77, 0x4c2a, { 0xb3, 0xdc, 0x27, 0xb, 0x7b, 0xa9, 0xe4, 0xb0} \
  }

static EFI_GUID  gFspInfoHeaderGuid = FSP_INFO_HEADER_GUID;
#ifdef __PRE_RAM__
static EFI_GUID  gEfiMemoryConfigDataGuid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
#endif

/*
  * Compares two GUIDs.
  * Input:
  *   Guid1       a 128 bit GUID.
  *   Guid2       a 128 bit GUID.
  * Return:
  *   TRUE      Guid1 and Guid2 are identical.
  *   FALSE     Guid1 and Guid2 are not identical.
 */
#define CompareGuid(Guid1, Guid2) (	\
	(Guid1.Data1 == Guid2.Data1) &&	\
	(Guid1.Data2 == Guid2.Data2) &&	\
	(Guid1.Data3 == Guid2.Data3) &&	\
	(Guid1.Data4[0] == Guid2.Data4[0]) &&	\
	(Guid1.Data4[1] == Guid2.Data4[1]) &&	\
	(Guid1.Data4[2] == Guid2.Data4[2]) &&	\
	(Guid1.Data4[3] == Guid2.Data4[3]) &&	\
	(Guid1.Data4[4] == Guid2.Data4[4]) &&	\
	(Guid1.Data4[5] == Guid2.Data4[5]) &&	\
	(Guid1.Data4[6] == Guid2.Data4[6]) &&	\
	(Guid1.Data4[7] == Guid2.Data4[7])	\
  )

/*
  * Compares two GUIDs.
  * Input:
  *   Guid1       A pointer to a 128 bit GUID.
  *   Guid2       A pointer to a 128 bit GUID.
  * Return:
  *   TRUE      Guid1 and Guid2 are identical.
  *   FALSE     Guid1 and Guid2 are not identical.
 */
#define CompareGuidPtr(Guid1, Guid2) (	\
        (((UINT32 *) Guid1)[0] == ((UINT32 *) Guid2)[0]) && \
        (((UINT32 *) Guid1)[1] == ((UINT32 *) Guid2)[1]) && \
        (((UINT32 *) Guid1)[2] == ((UINT32 *) Guid2)[2]) && \
        (((UINT32 *) Guid1)[3] == ((UINT32 *) Guid2)[3]) \
  )

/*
 * Platform configuration pointer/structure here.
 */

#ifdef __PRE_RAM__
static void ContinuationFunc (EFI_STATUS Status, void *HobListPtr)
{
  /* Should add the necessary code here! */

  EFI_HOB_GUID_TYPE            *GuidHob;

  /*
   * DO NOT know why the following code always fail to build.
   *
   * GuidHob = GetNextGuidHob (&gEfiMemoryConfigDataGuid, HobListPtr);
   *
   * The error message as below,
   *
   *   .data is non-zero size in romstage which is not allowed -- global variable?
   *
   * OR,
   *
   *   .bss is non-zero size in romstage which is not allowed -- global variable?
   *
   * It's replaced with the following code.
   */

  /* Get the Memory Configure Data HOB */
  EFI_PEI_HOB_POINTERS      mGuidHob;

  mGuidHob.Raw = (UINT8 *) HobListPtr;
  while ((mGuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, mGuidHob.Raw)) != NULL) {
    if (CompareGuid(gEfiMemoryConfigDataGuid, mGuidHob.Guid->Name)) {
      break;
    }
    mGuidHob.Raw = GET_NEXT_HOB (mGuidHob);
  }
  GuidHob = (EFI_HOB_GUID_TYPE *) mGuidHob.Raw;

  if (GuidHob == NULL) {

    /* Avoid store Hob to CBMEM if NVRAM Hob not exist */
    printk(BIOS_DEBUG, "FSP NVRAM Hob NOT found.\n");

  } else {

  /* Get Guid Hob size and Guid Hob Data pointer */
  void             *GuidHobData;
  uint32_t         GuidHobSize;
  GuidHobData = (VOID *)(*(UINT8 **)&(mGuidHob.Raw) + sizeof (EFI_HOB_GUID_TYPE));
  GuidHobSize = (uint32_t) ((((EFI_HOB_GUID_TYPE *)(mGuidHob.Raw))->Header.HobLength) - sizeof(EFI_HOB_GUID_TYPE));

  /* DEBUG INFO */
  printk(BIOS_DEBUG, "FSP NVRAM Hob at 0x%x (size = 0x%x).\n", (uint32_t)GuidHobData, (uint32_t)GuidHobSize);

#if CONFIG_EARLY_CBMEM_INIT
  struct mrc_data_container *mrcdata;
  int output_len = ALIGN(GuidHobSize, 16);

  /* Save the MRC S3/Fastboot/ADR restore data to cbmem */
  cbmem_initialize();
  mrcdata = cbmem_add
      (CBMEM_ID_MRCDATA,
      output_len + sizeof(struct mrc_data_container));

  printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
      (void *)GuidHobData, mrcdata, output_len);

  mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
  mrcdata->mrc_data_size = output_len;
  mrcdata->reserved = 0;
  memcpy(mrcdata->mrc_data, (void *)GuidHobData,
      GuidHobSize);

  /* Zero the unused space in aligned buffer. */
  if (output_len > GuidHobSize)
      memset(mrcdata->mrc_data + GuidHobSize, 0,
         output_len - GuidHobSize);

  mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
      mrcdata->mrc_data_size);
#endif
  }

  /* Rangeley early init */
  rangeley_early_initialization();

  /* Notify FSP for PostPciEnumeration */
  FspNotifyPhase (EnumInitPhaseAfterPciEnumeration);

  /* Return to coreboot boot process. */
  __asm__ __volatile__ (
      "jmp fsp_init_done\n\t"
    );
}

/** C Based Basic Initialization
 *
 * Platform configuration with Temp Stack starts here.
 *
 */
void early_init (FSP_INFO_HEADER *fsp_info)
{
  FSP_FSP_INIT              FspInitApi;
  FSP_INIT_PARAMS           FspInitParams;
  FSP_INIT_RT_BUFFER        FspRtBuffer;
  VPD_DATA_REGION     *FspVpdRgn;
  UPD_DATA_REGION     FspUpdRgn;
  struct mrc_data_container *mrc_cache;

  memset((void*)&FspRtBuffer, 0, sizeof(FSP_INIT_RT_BUFFER));
  FspRtBuffer.Common.StackTop = (uint32_t *) ROMSTAGE_STACK;
  FspRtBuffer.Common.UpdDataRgnPtr = (UPD_DATA_REGION *)&FspUpdRgn;

  /* find_current_mrc_cache */
  if (((mrc_cache = find_current_mrc_cache()) == NULL) || (mrc_cache->mrc_data_size == -1UL)) {
    printk(BIOS_DEBUG, "MRC cache not present.\n");
    FspInitParams.NvsBufferPtr = (void *) NULL;
    FspRtBuffer.Common.BootMode = BOOT_WITH_FULL_CONFIGURATION;
  } else {
    printk(BIOS_DEBUG, "MRC cache present at %x.\n", (uint32_t)mrc_cache);
    FspInitParams.NvsBufferPtr = (void *) (EFI_HOB_GUID_TYPE *) mrc_cache->mrc_data;
    FspRtBuffer.Common.BootMode = BOOT_WITH_FULL_CONFIGURATION;
  }

  /* Get VPD region start */
  FspVpdRgn = (VPD_DATA_REGION *)(fsp_info->ImageBase + fsp_info->CfgRegionOffset);

  /* Verifify the VPD data region is valid */
  ASSERT((FspVpdRgn->PcdImageRevision == VPD_IMAGE_REV) && (FspVpdRgn->PcdVpdRegionSign == VPD_IMAGE_ID));

  /* Copy default data from Flash */
  memcpy ((void*)&FspUpdRgn, (void *)(fsp_info->ImageBase + FspVpdRgn->PcdUpdRegionOffset), sizeof(UPD_DATA_REGION));

  /* Verifify the UPD data region is valid */
  ASSERT(FspUpdRgn.PcdRegionTerminator == 0x55AA);

  /* Override any UPD setting if required */
  UpdateFspUpdConfigs (&FspUpdRgn);

#if CONFIG_MEMORY_DOWN_ENABLE
 /*
  * Setup platform specific structure
  */

  /* Channel0/Dimm0, DIMM populated */
  FspRtBuffer.Platform.MemDownDimmConfig[0][0] = (MEM_DOWN_DIMM_CONFIG *)&OemMemoryDownDimmConfig;
  /* Channel0/Dimm1, not populated */
  FspRtBuffer.Platform.MemDownDimmConfig[0][1] = (MEM_DOWN_DIMM_CONFIG *)&DummyMemoryDownDimmConfig;
  /* Channel1/Dimm0, DIMM populated */
  FspRtBuffer.Platform.MemDownDimmConfig[1][0] = (MEM_DOWN_DIMM_CONFIG *)&OemMemoryDownDimmConfig;
  /* Channel1/Dimm1, not populated */
  FspRtBuffer.Platform.MemDownDimmConfig[1][1] = (MEM_DOWN_DIMM_CONFIG *)&DummyMemoryDownDimmConfig;
#endif

  FspInitParams.RtBufferPtr  = (FSP_INIT_RT_BUFFER *)&FspRtBuffer;
  FspInitParams.ContinuationFunc = (CONTINUATION_PROC)ContinuationFunc;
  FspInitApi = (FSP_FSP_INIT)(fsp_info->ImageBase + fsp_info->FspInitEntry);
  FspInitApi(&FspInitParams);

  /* Should never return. Control will continue from ContinuationFunc */
  while (1);
}
#endif

/** C Based Basic Initialization
 *
 * Find FSP header
 *
 */
void __attribute__((optimize("O0"))) find_fsp_header ()
{
  volatile register UINT8 *ptr asm ("eax");

#ifdef __PRE_RAM__
  __asm__ __volatile__ (
      ".global find_fsp_entry\n\t"
      "find_fsp_entry:\n\t"
    );
#endif

  //
  // Start at the FSP / FV Header base
  //
  ptr = (UINT8 *)CONFIG_FSP_BIN_BASE;

  //
  // Validate FV signature _FVH
  //
  if (((EFI_FIRMWARE_VOLUME_HEADER *)ptr)-> Signature != EFI_FVH_SIGNATURE) {
    ptr = 0;
    goto NotFound;
  }

  //
  // Add the Ext Header size to the Ext Header base to go to the end of FV
  // header
  //
  ptr += ((EFI_FIRMWARE_VOLUME_HEADER *)ptr)->ExtHeaderOffset;
  ptr += ((EFI_FIRMWARE_VOLUME_EXT_HEADER *)ptr)->ExtHeaderSize;

  //
  // Align the end of FV header address to 8 bytes
  //
  ptr  = (UINT8 *)(((uint32_t)ptr + 7) & 0xFFFFFFF8);

  //
  // Now ptr is pointing to thr FFS Header. Verify if the GUID
  // matches the FSP_INFO_HEADER GUID
  //
  if (!CompareGuid(((EFI_GUID) (((EFI_FFS_FILE_HEADER *)ptr)->Name)), ((EFI_GUID) gFspInfoHeaderGuid))) {
      ptr = 0;
      goto NotFound;
  }

  //
  // Add the FFS Header size to the base to find the Raw section Header
  //
  ptr += sizeof(EFI_FFS_FILE_HEADER);
  if (((EFI_RAW_SECTION *)ptr)->Type != EFI_SECTION_RAW) {
      ptr = 0;
      goto NotFound;
  }

  //
  // Add the Raw Header size to the base to find the FSP INFO Header
  //
  ptr += sizeof(EFI_RAW_SECTION);

NotFound:
  __asm__ __volatile__ ("ret");
}

/** C Based Basic Initialization
 *
 * Notify Phase for different stage.
 *
 */
void FspNotifyPhase (uint32_t Phase)
{
    FSP_NOTFY_PHASE        NotifyPhaseProc;
    NOTIFY_PHASE_PARAMS    NotifyPhaseParams;
    FSP_INFO_HEADER *fsp_info_header;
    EFI_STATUS             Status;

  __asm__ __volatile__ (
        "call  find_fsp_header      \n\t" /* Call FSP Notify API */
        "movl %%eax, %0\n\t"
	: "=m"(fsp_info_header) ::
	);

    /* call FSP PEI to Notify PostPciEnumeration */
    NotifyPhaseProc   = (FSP_NOTFY_PHASE)(fsp_info_header->ImageBase + fsp_info_header->NotifyPhaseEntry);
    NotifyPhaseParams.Phase = Phase;
    Status = NotifyPhaseProc (&NotifyPhaseParams);
    if (Status != 0) {
	printk(BIOS_DEBUG, "FSP API NotifyPhase failed for phase %d!\n", Phase);
    }
}

/*
 * Board Specific routine before handling over to boot loader.
 */
#if !defined(__PRE_RAM__)
void mainboard_cbmem_post_handling(void)
{

  /* Notify FSP for ReadyToBoot */
  FspNotifyPhase (EnumInitPhaseReadyToBoot);
}
#endif

/**
  This function overrides the default configurations in the UPD data region.

  @param   UpdDataRgn   A pointer to the UPD_DATA_REGION data strcture.

  @return  None
**/
VOID
UpdateFspUpdConfigs (
  UPD_DATA_REGION        *FspUpdRgn
)
{
  /* Override any UPD setting if required */

  //
  // Uncomment the line below to disable LAN device
  //
  //FspUpdRgn->PcdEnableLan = 0;

}
OpenPOWER on IntegriCloud