sdram.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2017 Intel Corporation
  4. */
  5. #include <common.h>
  6. #include <asm/e820.h>
  7. #include <asm/global_data.h>
  8. #include <asm/sfi.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. /*
  11. * SFI tables are part of the first stage bootloader.
  12. *
  13. * U-Boot finds the System Table by searching 16-byte boundaries between
  14. * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
  15. * starting at the low address and shall stop searching when the 1st valid SFI
  16. * System Table is found.
  17. */
  18. #define SFI_BASE_ADDR 0x000E0000
  19. #define SFI_LENGTH 0x00020000
  20. #define SFI_TABLE_LENGTH 16
  21. static int sfi_table_check(struct sfi_table_header *sbh)
  22. {
  23. char chksum = 0;
  24. char *pos = (char *)sbh;
  25. u32 i;
  26. if (sbh->len < SFI_TABLE_LENGTH)
  27. return -ENXIO;
  28. if (sbh->len > SFI_LENGTH)
  29. return -ENXIO;
  30. for (i = 0; i < sbh->len; i++)
  31. chksum += *pos++;
  32. if (chksum)
  33. pr_err("sfi: Invalid checksum\n");
  34. /* Checksum is OK if zero */
  35. return chksum ? -EILSEQ : 0;
  36. }
  37. static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
  38. {
  39. return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
  40. !sfi_table_check(sbh);
  41. }
  42. static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
  43. const char *signature)
  44. {
  45. struct sfi_table_simple *sb;
  46. u32 i;
  47. for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
  48. sb = (struct sfi_table_simple *)(addr + i);
  49. if (sfi_table_is_type(&sb->header, signature))
  50. return sb;
  51. }
  52. return NULL;
  53. }
  54. static struct sfi_table_simple *sfi_search_mmap(void)
  55. {
  56. struct sfi_table_header *sbh;
  57. struct sfi_table_simple *sb;
  58. u32 sys_entry_cnt;
  59. u32 i;
  60. /* Find SYST table */
  61. sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
  62. if (!sb) {
  63. pr_err("sfi: failed to locate SYST table\n");
  64. return NULL;
  65. }
  66. sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
  67. /* Search through each SYST entry for MMAP table */
  68. for (i = 0; i < sys_entry_cnt; i++) {
  69. sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
  70. if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
  71. return (struct sfi_table_simple *)sbh;
  72. }
  73. pr_err("sfi: failed to locate SFI MMAP table\n");
  74. return NULL;
  75. }
  76. #define sfi_for_each_mentry(i, sb, mentry) \
  77. for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
  78. i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
  79. i++, mentry++) \
  80. static unsigned int sfi_setup_e820(unsigned int max_entries,
  81. struct e820_entry *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 int install_e820_map(unsigned int max_entries,
  153. struct e820_entry *entries)
  154. {
  155. return sfi_setup_e820(max_entries, entries);
  156. }
  157. int dram_init_banksize(void)
  158. {
  159. sfi_get_bank_size();
  160. return 0;
  161. }
  162. int dram_init(void)
  163. {
  164. gd->ram_size = sfi_get_ram_size();
  165. return 0;
  166. }