sdram.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2017 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/e820.h>
  8. #include <asm/global_data.h>
  9. #include <asm/sfi.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. /*
  12. * SFI tables are part of the first stage bootloader.
  13. *
  14. * U-Boot finds the System Table by searching 16-byte boundaries between
  15. * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
  16. * starting at the low address and shall stop searching when the 1st valid SFI
  17. * System Table is found.
  18. */
  19. #define SFI_BASE_ADDR 0x000E0000
  20. #define SFI_LENGTH 0x00020000
  21. #define SFI_TABLE_LENGTH 16
  22. static int sfi_table_check(struct sfi_table_header *sbh)
  23. {
  24. char chksum = 0;
  25. char *pos = (char *)sbh;
  26. u32 i;
  27. if (sbh->len < SFI_TABLE_LENGTH)
  28. return -ENXIO;
  29. if (sbh->len > SFI_LENGTH)
  30. return -ENXIO;
  31. for (i = 0; i < sbh->len; i++)
  32. chksum += *pos++;
  33. if (chksum)
  34. error("sfi: Invalid checksum\n");
  35. /* Checksum is OK if zero */
  36. return chksum ? -EILSEQ : 0;
  37. }
  38. static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
  39. {
  40. return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
  41. !sfi_table_check(sbh);
  42. }
  43. static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
  44. const char *signature)
  45. {
  46. struct sfi_table_simple *sb;
  47. u32 i;
  48. for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
  49. sb = (struct sfi_table_simple *)(addr + i);
  50. if (sfi_table_is_type(&sb->header, signature))
  51. return sb;
  52. }
  53. return NULL;
  54. }
  55. static struct sfi_table_simple *sfi_search_mmap(void)
  56. {
  57. struct sfi_table_header *sbh;
  58. struct sfi_table_simple *sb;
  59. u32 sys_entry_cnt;
  60. u32 i;
  61. /* Find SYST table */
  62. sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
  63. if (!sb) {
  64. error("sfi: failed to locate SYST table\n");
  65. return NULL;
  66. }
  67. sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
  68. /* Search through each SYST entry for MMAP table */
  69. for (i = 0; i < sys_entry_cnt; i++) {
  70. sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
  71. if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
  72. return (struct sfi_table_simple *)sbh;
  73. }
  74. error("sfi: failed to locate SFI MMAP table\n");
  75. return NULL;
  76. }
  77. #define sfi_for_each_mentry(i, sb, mentry) \
  78. for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
  79. i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
  80. i++, mentry++) \
  81. static unsigned sfi_setup_e820(unsigned max_entries, struct e820entry *entries)
  82. {
  83. struct sfi_table_simple *sb;
  84. struct sfi_mem_entry *mentry;
  85. unsigned long long start, end, size;
  86. int type, total = 0;
  87. u32 i;
  88. sb = sfi_search_mmap();
  89. if (!sb)
  90. return 0;
  91. sfi_for_each_mentry(i, sb, mentry) {
  92. start = mentry->phys_start;
  93. size = mentry->pages << 12;
  94. end = start + size;
  95. if (start > end)
  96. continue;
  97. /* translate SFI mmap type to E820 map type */
  98. switch (mentry->type) {
  99. case SFI_MEM_CONV:
  100. type = E820_RAM;
  101. break;
  102. case SFI_MEM_UNUSABLE:
  103. case SFI_RUNTIME_SERVICE_DATA:
  104. continue;
  105. default:
  106. type = E820_RESERVED;
  107. }
  108. if (total == E820MAX)
  109. break;
  110. entries[total].addr = start;
  111. entries[total].size = size;
  112. entries[total].type = type;
  113. total++;
  114. }
  115. return total;
  116. }
  117. static int sfi_get_bank_size(void)
  118. {
  119. struct sfi_table_simple *sb;
  120. struct sfi_mem_entry *mentry;
  121. int bank = 0;
  122. u32 i;
  123. sb = sfi_search_mmap();
  124. if (!sb)
  125. return 0;
  126. sfi_for_each_mentry(i, sb, mentry) {
  127. if (mentry->type != SFI_MEM_CONV)
  128. continue;
  129. gd->bd->bi_dram[bank].start = mentry->phys_start;
  130. gd->bd->bi_dram[bank].size = mentry->pages << 12;
  131. bank++;
  132. }
  133. return bank;
  134. }
  135. static phys_size_t sfi_get_ram_size(void)
  136. {
  137. struct sfi_table_simple *sb;
  138. struct sfi_mem_entry *mentry;
  139. phys_size_t ram = 0;
  140. u32 i;
  141. sb = sfi_search_mmap();
  142. if (!sb)
  143. return 0;
  144. sfi_for_each_mentry(i, sb, mentry) {
  145. if (mentry->type != SFI_MEM_CONV)
  146. continue;
  147. ram += mentry->pages << 12;
  148. }
  149. debug("sfi: RAM size %llu\n", ram);
  150. return ram;
  151. }
  152. unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
  153. {
  154. return sfi_setup_e820(max_entries, entries);
  155. }
  156. int dram_init_banksize(void)
  157. {
  158. sfi_get_bank_size();
  159. return 0;
  160. }
  161. int dram_init(void)
  162. {
  163. gd->ram_size = sfi_get_ram_size();
  164. return 0;
  165. }