Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Memory/spd.c

1/*
2 * spd.c - serial presence detect memory information
3 *
4 * Originally restored from pcefi10.5
5 * Dynamic mem detection original impl. by Rekursor
6 * System profiler fix and other fixes by Mozodojo.
7 */
8
9#include "libsaio.h"
10#include "pci.h"
11#include "platform.h"
12#include "spd.h"
13#include "saio_internal.h"
14#include "bootstruct.h"
15#include "memvendors.h"
16
17#ifndef DEBUG_SPD
18#define DEBUG_SPD 0
19#endif
20
21#if DEBUG_SPD
22#define DBG(x...)printf(x)
23#include "mem.h"
24#else
25#define DBG(x...)
26#endif
27
28const char *spd_memory_types[] =
29{
30"RAM", /* 00h Undefined */
31"FPM", /* 01h FPM */
32"EDO", /* 02h EDO */
33"",/* 03h PIPELINE NIBBLE */
34"SDRAM", /* 04h SDRAM */
35"",/* 05h MULTIPLEXED ROM */
36"DDR SGRAM",/* 06h SGRAM DDR */
37"DDR SDRAM",/* 07h SDRAM DDR */
38"DDR2 SDRAM", /* 08h SDRAM DDR 2 */
39"",/* 09h Undefined */
40"",/* 0Ah Undefined */
41"DDR3 SDRAM" /* 0Bh SDRAM DDR 3 */
42};
43
44#define UNKNOWN_MEM_TYPE 2
45uint8_t spd_mem_to_smbios[] =
46{
47UNKNOWN_MEM_TYPE, /* 00h Undefined */
48UNKNOWN_MEM_TYPE, /* 01h FPM */
49UNKNOWN_MEM_TYPE, /* 02h EDO */
50UNKNOWN_MEM_TYPE, /* 03h PIPELINE NIBBLE */
51SMB_MEM_TYPE_SDRAM, /* 04h SDRAM */
52SMB_MEM_TYPE_ROM, /* 05h MULTIPLEXED ROM */
53SMB_MEM_TYPE_SGRAM, /* 06h SGRAM DDR */
54SMB_MEM_TYPE_DDR, /* 07h SDRAM DDR */
55SMB_MEM_TYPE_DDR2, /* 08h SDRAM DDR 2 */
56UNKNOWN_MEM_TYPE, /* 09h Undefined */
57UNKNOWN_MEM_TYPE, /* 0Ah Undefined */
58SMB_MEM_TYPE_DDR3 /* 0Bh SDRAM DDR 3 */
59};
60#define SPD_TO_SMBIOS_SIZE (sizeof(spd_mem_to_smbios)/sizeof(uint8_t))
61
62#define rdtsc(low,high) \
63__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
64
65#define SMBHSTSTS 0
66#define SMBHSTCNT 2
67#define SMBHSTCMD 3
68#define SMBHSTADD 4
69#define SMBHSTDAT 5
70#define SBMBLKDAT 7
71
72/** Read one byte from the intel i2c, used for reading SPD on intel chipsets only. */
73unsigned char smb_read_byte_intel(uint32_t base, uint8_t adr, uint8_t cmd)
74{
75 int l1 = 0;
76int l2 = 0;
77int h1 = 0;
78int h2 = 0;
79 unsigned long long t;
80
81 outb(base + SMBHSTSTS, 0x1f);// reset SMBus Controller
82 outb(base + SMBHSTDAT, 0xff);
83
84 while( inb(base + SMBHSTSTS) & 0x01)
85{
86rdtsc(l2, h2);
87t = ((h2 - h1) * 0xffffffff + (l2 - l1)) / (Platform->CPU.TSCFrequency / 100);
88if(t > 50) return 0xFF;// hack, exit if unresponsive.
89}
90
91 outb(base + SMBHSTCMD, cmd);
92 outb(base + SMBHSTADD, (adr << 1) | 0x01 );
93 outb(base + SMBHSTCNT, 0x48 );
94
95 rdtsc(l1, h1);
96
97 while (!( inb(base + SMBHSTSTS) & 0x02))// wait til command finished
98{
99rdtsc(l2, h2);
100t = ((h2 - h1) * 0xffffffff + (l2 - l1)) / (Platform->CPU.TSCFrequency / 100);
101if (t > 5)
102break;// break after 5ms
103 }
104 return inb(base + SMBHSTDAT);
105}
106
107/* SPD i2c read optimization: prefetch only what we need, read non prefetcheable bytes on the fly */
108#define READ_SPD(spd, base, slot, x) spd[x] = smb_read_byte_intel(base, 0x50 + slot, x)
109
110int spd_indexes[] = {
111SPD_MEMORY_TYPE,
112SPD_DDR3_MEMORY_BANK,
113SPD_DDR3_MEMORY_CODE,
114SPD_NUM_ROWS,
115SPD_NUM_COLUMNS,
116SPD_NUM_DIMM_BANKS,
117SPD_NUM_BANKS_PER_SDRAM,
1184,7,8,9,12,64, /* TODO: give names to these values */
11995,96,97,98, 122,123,124,125 /* UIS */
120};
121#define SPD_INDEXES_SIZE (sizeof(spd_indexes) / sizeof(int))
122
123/** Read from spd *used* values only*/
124static void init_spd(char * spd, uint32_t base, int slot)
125{
126int i;
127for (i=0; i< SPD_INDEXES_SIZE; i++) {
128READ_SPD(spd, base, slot, spd_indexes[i]);
129}
130}
131
132/** Get Vendor Name from spd, 2 cases handled DDR3 and DDR2,
133 have different formats, always return a valid ptr.*/
134const char * getVendorName(RamSlotInfo_t* slot, uint32_t base, int slot_num)
135{
136 uint8_t bank = 0;
137 uint8_t code = 0;
138 int i = 0;
139 uint8_t * spd = (uint8_t *) slot->spd;
140
141 if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR3) { // DDR3
142 bank = (spd[SPD_DDR3_MEMORY_BANK] & 0x07f); // constructors like Patriot use b7=1
143 code = spd[SPD_DDR3_MEMORY_CODE];
144 for (i=0; i < VEN_MAP_SIZE; i++)
145 if (bank==vendorMap[i].bank && code==vendorMap[i].code)
146 return vendorMap[i].name;
147 }
148 else if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR2) {
149 if(spd[64]==0x7f) {
150 for (i=64; i<72 && spd[i]==0x7f;i++) {
151bank++;
152READ_SPD(spd, base, slot_num,i+1); // prefetch next spd byte to read for next loop
153}
154READ_SPD(spd, base, slot_num,i);
155 code = spd[i];
156 } else {
157 code = spd[64];
158 bank = 0;
159 }
160 for (i=0; i < VEN_MAP_SIZE; i++)
161 if (bank==vendorMap[i].bank && code==vendorMap[i].code)
162 return vendorMap[i].name;
163 }
164 /* OK there is no vendor id here lets try to match the partnum if it exists */
165 if (strstr(slot->PartNo,"GU332") == slot->PartNo) // Unifosa fingerprint
166 return "Unifosa";
167 return "NoName";
168}
169
170/** Get Default Memory Module Speed (no overclocking handled) */
171int getDDRspeedMhz(const char * spd)
172{
173 if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR3) {
174 switch(spd[12]) {
175case 0x0f:
176return 1066;
177case 0x0c:
178return 1333;
179case 0x0a:
180return 1600;
181case 0x14:
182default:
183return 800;
184 }
185 }
186 else if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR2) {
187 switch(spd[9]) {
188case 0x50:
189return 400;
190case 0x3d:
191return 533;
192case 0x30:
193return 667;
194case 0x25:
195default:
196return 800;
197 }
198 }
199 return 800; // default freq for unknown types
200}
201
202#define SMST(a) ((uint8_t)((spd[a] & 0xf0) >> 4))
203#define SLST(a) ((uint8_t)(spd[a] & 0x0f))
204
205/** Get DDR3 or DDR2 serial number, 0 most of the times, always return a valid ptr */
206char asciiSerial[16];
207const char *getDDRSerial(const char* spd)
208{
209
210 if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR3) // DDR3
211 {
212sprintf(asciiSerial, "%X%X%X%X%X%X%X%X", SMST(122) /*& 0x7*/, SLST(122), SMST(123), SLST(123), SMST(124), SLST(124), SMST(125), SLST(125));
213 }
214 else if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR2) // DDR2 or DDR
215 {
216sprintf(asciiSerial, "%X%X%X%X%X%X%X%X", SMST(95) /*& 0x7*/, SLST(95), SMST(96), SLST(96), SMST(97), SLST(97), SMST(98), SLST(98));
217 }
218
219 return strdup(asciiSerial);
220}
221
222/** Get DDR3 or DDR2 Part Number, always return a valid ptr */
223char asciiPartNo[32];
224const char * getDDRPartNum(char* spd, uint32_t base, int slot)
225{
226int i, start=0, index = 0;
227
228 if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR3) {
229start = 128;
230}
231 else if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR2) {
232start = 73;
233}
234
235 // Check that the spd part name is zero terminated and that it is ascii:
236 bzero(asciiPartNo, sizeof(asciiPartNo));
237char c;
238for (i=start; i < start + sizeof(asciiPartNo); i++) {
239READ_SPD(spd, base, slot, i); // only read once the corresponding model part (ddr3 or ddr2)
240c = spd[i];
241if (isalpha(c) || isdigit(c) || ispunct(c)) // It seems that System Profiler likes only letters and digits...
242asciiPartNo[index++] = c;
243else if (!isascii(c))
244break;
245}
246
247return strdup(asciiPartNo);
248 return NULL;
249}
250
251int mapping []= {0,2,1,3,4,6,5,7,8,10,9,11};
252
253
254/** Read from smbus the SPD content and interpret it for detecting memory attributes */
255static void read_smb_intel(pci_dt_t *smbus_dev)
256{
257 int i, speed;
258 uint8_t spd_size, spd_type;
259 uint32_t base;
260 bool dump = false;
261 RamSlotInfo_t* slot;
262
263 base = pci_config_read16(smbus_dev->dev.addr, 0x20) & 0xFFFE;
264 DBG("Scanning smbus_dev <%04x, %04x> ...\n",smbus_dev->vendor_id, smbus_dev->device_id);
265
266 getBoolForKey("DumpSPD", &dump, &bootInfo->bootConfig);
267 bool fullBanks = // needed at least for laptops
268Platform->DMI.MemoryModules == Platform->DMI.MaxMemorySlots;
269// Search MAX_RAM_SLOTS slots
270char spdbuf[256];
271
272 for (i = 0; i < MAX_RAM_SLOTS; i++){
273DBG("Scanning slot %d\n", i);
274 slot = &Platform->RAM.DIMM[i];
275 spd_size = smb_read_byte_intel(base, 0x50 + i, 0);
276 // Check spd is present
277 if (spd_size && (spd_size != 0xff) ) {
278slot->spd = spdbuf;
279 slot->InUse = true;
280
281 bzero(slot->spd, spd_size);
282
283 // Copy spd data into buffer
284
285//for (x = 0; x < spd_size; x++) slot->spd[x] = smb_read_byte_intel(base, 0x50 + i, x);
286 init_spd(slot->spd, base, i);
287
288 switch (slot->spd[SPD_MEMORY_TYPE]) {
289case SPD_MEMORY_TYPE_SDRAM_DDR2:
290
291slot->ModuleSize = ((1 << (slot->spd[SPD_NUM_ROWS] & 0x0f) + (slot->spd[SPD_NUM_COLUMNS] & 0x0f) - 17) *
292((slot->spd[SPD_NUM_DIMM_BANKS] & 0x7) + 1) * slot->spd[SPD_NUM_BANKS_PER_SDRAM]);
293break;
294
295case SPD_MEMORY_TYPE_SDRAM_DDR3:
296
297slot->ModuleSize = ((slot->spd[4] & 0x0f) + 28 ) + ((slot->spd[8] & 0x7) + 3 );
298slot->ModuleSize -= (slot->spd[7] & 0x7) + 25;
299slot->ModuleSize = ((1 << slot->ModuleSize) * (((slot->spd[7] >> 3) & 0x1f) + 1));
300
301break;
302 }
303
304 spd_type = (slot->spd[SPD_MEMORY_TYPE] < ((char) 12) ? slot->spd[SPD_MEMORY_TYPE] : 0);
305 slot->Type = spd_mem_to_smbios[spd_type];
306 slot->PartNo = getDDRPartNum(slot->spd, base, i);
307 slot->Vendor = getVendorName(slot, base, i);
308 slot->SerialNo = getDDRSerial(slot->spd);
309
310 // determine spd speed
311 speed = getDDRspeedMhz(slot->spd);
312 if (slot->Frequency<speed) slot->Frequency = speed;
313
314// pci memory controller if available, is more reliable
315if (Platform->RAM.Frequency > 0) {
316uint32_t freq = (uint32_t)Platform->RAM.Frequency / 500000;
317// now round off special cases
318uint32_t fmod100 = freq %100;
319switch(fmod100) {
320case 1:freq--;break;
321case 32:freq++;break;
322case 65:freq++; break;
323case 98:freq+=2;break;
324case 99:freq++; break;
325}
326slot->Frequency = freq;
327}
328
329verbose("Slot: %d Type %d %dMB (%s) %dMHz Vendor=%s\n PartNo=%s SerialNo=%s\n",
330i,
331(int)slot->Type,
332slot->ModuleSize,
333spd_memory_types[spd_type],
334slot->Frequency,
335slot->Vendor,
336slot->PartNo,
337slot->SerialNo);
338#if DEBUG_SPD
339dumpPhysAddr("spd content: ",slot->spd, spd_size);
340getc();
341#endif
342 }
343 // laptops sometimes show slot 0 and 2 with slot 1 empty when only 2 slots are presents so:
344 Platform->DMI.DIMM[i]=
345i>0 && Platform->RAM.DIMM[1].InUse==false && fullBanks && Platform->DMI.MaxMemorySlots==2 ?
346mapping[i] : i; // for laptops case, mapping setup would need to be more generic than this
347
348slot->spd = NULL;
349
350 } // for
351}
352
353struct smbus_controllers_t smbus_controllers[] = {
354
355{0x8086, 0x269B, "ESB2", read_smb_intel },
356{0x8086, 0x25A4, "6300ESB", read_smb_intel },
357{0x8086, 0x24C3, "ICH4", read_smb_intel },
358{0x8086, 0x24D3, "ICH5", read_smb_intel },
359{0x8086, 0x266A, "ICH6", read_smb_intel },
360{0x8086, 0x27DA, "ICH7", read_smb_intel },
361{0x8086, 0x283E, "ICH8", read_smb_intel },
362{0x8086, 0x2930, "ICH9", read_smb_intel },
363{0x8086, 0x3A30, "ICH10R", read_smb_intel },
364{0x8086, 0x3A60, "ICH10B", read_smb_intel },
365{0x8086, 0x3B30, "P55", read_smb_intel },
366{0x8086, 0x5032, "EP80579", read_smb_intel }
367
368};
369
370
371bool is_smbus_controller(pci_dt_t* pci_dt)
372{
373int i = 0;
374for ( ; i < sizeof(smbus_controllers) / sizeof(smbus_controllers[0]); i++ )
375{
376if (pci_dt->vendor_id == smbus_controllers[i].vendor &&
377pci_dt->device_id == smbus_controllers[i].device)
378{
379return true;
380}
381}
382return false;
383}
384
385void scan_spd(PlatformInfo_t *p, pci_dt_t* smbus_controller_dev)
386{
387int i = 0;
388for ( ; i < sizeof(smbus_controllers) / sizeof(smbus_controllers[0]); i++ )
389{
390if (smbus_controller_dev->vendor_id == smbus_controllers[i].vendor &&
391smbus_controller_dev->device_id == smbus_controllers[i].device)
392{
393smbus_controllers[i].read_smb(smbus_controller_dev); // read smb
394}
395}
396}
397

Archive Download this file

Revision: 789