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