blob: 16c1d8920c4fdc0942bd854e346b44e4a118b16f [file] [log] [blame]
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +02001/*
2 * Copyright (C) 2014 Vladimir Serbinenko
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 or (at your option) any later version of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +020012 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <inttypes.h>
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +020018#include <errno.h>
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +020019#include "inteltool.h"
20
21extern volatile uint8_t *mchbar;
22
23static uint32_t read_mchbar32(uint32_t addr)
24{
25 return *(volatile uint32_t *)(mchbar + addr);
26}
27
28static void
29print_time(const char *string, unsigned long long time, unsigned long long tCK)
30{
31 printf(".%s = %lld /* %lld clocks = %.3lf ns */,\n",
32 string, time, time, (time * tCK) / 256.0);
33}
34
35static unsigned int
36make_spd_time(unsigned long long time, unsigned long long tCK)
37{
38 return (time * tCK) >> 5;
39}
40
41static u16 spd_ddr3_calc_crc(u8 * spd)
42{
43 int n_crc, i;
44 u8 *ptr;
45 u16 crc;
46
47 /* Find the number of bytes covered by CRC */
48 if (spd[0] & 0x80) {
49 n_crc = 117;
50 } else {
51 n_crc = 126;
52 }
53
54 /* Compute the CRC */
55 crc = 0;
56 ptr = spd;
57 while (--n_crc >= 0) {
58 crc = crc ^ (int)*ptr++ << 8;
59 for (i = 0; i < 8; ++i)
60 if (crc & 0x8000) {
61 crc = crc << 1 ^ 0x1021;
62 } else {
63 crc = crc << 1;
64 }
65 }
66 return crc;
67}
68
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +020069void ivybridge_dump_timings(const char *dump_spd_file)
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +020070{
71 u32 mr0[2];
72 u32 mr1[2];
73 u32 reg;
74 unsigned int CAS = 0;
75 int tWR = 0, tRFC = 0;
76 int tFAW[2], tWTR[2], tCKE[2], tRTP[2], tRRD[2];
77 int channel, slot;
78 u32 reg_4004_b30[2] = { 0, 0 };
79 unsigned int tREFI;
80 int rankmap[2];
81 u32 mad_dimm[2];
82 unsigned int tRCD[2], tXP[2], tXPDLL[2], tRAS[2], tCWL[2], tRP[2],
83 tAONPD[2];
84 unsigned int tXSOffset;
85 int two_channels = 1;
86 struct slot_info {
87 unsigned int size_mb;
88 unsigned int ranks;
89 unsigned int width;
90 } slots[2][2];
91 unsigned int tCK;
92 u8 spd[2][2][256];
93
94 memset(slots, 0, sizeof(slots));
95
96 for (channel = 0; channel < 2; channel++) {
97 rankmap[channel] = read_mchbar32(0xc14 + 0x100 * channel) >> 24;
98 }
99
Jacob Garbera7544072019-05-09 14:24:02 -0600100 if ((rankmap[0] == 0) && (rankmap[1] == 0)) {
101 fputs("Error: no memory channels found\n", stderr);
102 exit(1);
103 }
104
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200105 two_channels = rankmap[0] && rankmap[1];
106
107 mr0[0] = read_mchbar32(0x0004);
108 mr1[0] = read_mchbar32(0x0008);
109 mr0[1] = read_mchbar32(0x0104);
110 mr1[1] = read_mchbar32(0x0108);
111
112 if (mr0[0] != mr0[1] && two_channels)
113 printf("MR0 mismatch: %x, %x\n", mr0[0], mr0[1]);
114 if (mr1[0] != mr1[1] && two_channels)
115 printf("MR1 mismatch: %x, %x\n", mr1[0], mr1[1]);
116
117 reg = read_mchbar32(0x5e00) & ~0x80000000;
118 printf(" .tCK = TCK_MHZ%d,\n", 400 * reg / 3);
119
120 tCK = (64 * 10 * 3) / reg;
121
122 if (mr0[0] & 1) {
123 CAS = ((mr0[0] >> 4) & 0x7) + 12;
124 } else {
125 CAS = ((mr0[0] >> 4) & 0x7) + 4;
126 }
127
128 for (channel = 0; channel < 2; channel++) {
129 mad_dimm[channel] = read_mchbar32(0x5004 + 4 * channel);
130 }
131
Elyes HAOUASde2918b2016-10-19 18:13:55 +0200132 printf(".rankmap = { 0x%x, 0x%x },\n", rankmap[0], rankmap[1]);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200133
Elyes HAOUASde2918b2016-10-19 18:13:55 +0200134 printf(".mad_dimm = { 0x%x, 0x%x },\n", mad_dimm[0], mad_dimm[1]);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200135
136 for (channel = 0; channel < 2; channel++)
137 if (rankmap[channel]) {
138 int ctWR;
139 static const u8 mr0_wr_t[12] =
140 { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
141 reg = read_mchbar32(0x4004 + 0x400 * channel);
142
143 ctWR = (reg >> 24) & 0x3f;
144 if (tWR && ctWR != tWR)
145 printf("/* tWR mismatch: %d, %d */\n", tWR,
146 ctWR);
147 if (!tWR)
148 tWR = ctWR;
149 if (((mr0[channel] >> 9) & 7) != mr0_wr_t[tWR - 5])
150 printf("/* encoded tWR mismatch: %d, %d */\n",
151 ((mr0[channel] >> 9) & 7),
152 mr0_wr_t[tWR - 5]);
153 reg_4004_b30[channel] = reg >> 30;
154 tFAW[channel] = (reg >> 16) & 0xff;
155 tWTR[channel] = (reg >> 12) & 0xf;
156 tCKE[channel] = (reg >> 8) & 0xf;
157 tRTP[channel] = (reg >> 4) & 0xf;
158 tRRD[channel] = (reg >> 0) & 0xf;
159
160 reg = read_mchbar32(0x4000 + 0x400 * channel);
161 tRAS[channel] = reg >> 16;
162 tCWL[channel] = (reg >> 12) & 0xf;
163 if (CAS != ((reg >> 8) & 0xf))
164 printf("/* CAS mismatch: %d, %d. */\n", CAS,
165 (reg >> 8) & 0xf);
166 tRP[channel] = (reg >> 4) & 0xf;
167 tRCD[channel] = reg & 0xf;
168
169 reg = read_mchbar32(0x400c + channel * 0x400);
170 tXPDLL[channel] = reg & 0x1f;
171 tXP[channel] = (reg >> 5) & 7;
172 tAONPD[channel] = (reg >> 8) & 0xff;
173 }
174 printf(".mobile = %d,\n", (mr0[0] >> 12) & 1);
175 print_time("CAS", CAS, tCK);
176 print_time("tWR", tWR, tCK);
177
178 printf(".reg_4004_b30 = { %d, %d },\n", reg_4004_b30[0],
179 reg_4004_b30[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600180
181 channel = (rankmap[0] != 0) ? 0 : 1;
182
Jacob Garber94d61ec2019-04-10 11:51:58 -0600183 if (two_channels && tFAW[0] != tFAW[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200184 printf("/* tFAW mismatch: %d, %d */\n", tFAW[0], tFAW[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600185 print_time("tFAW", tFAW[channel], tCK);
Jacob Garber94d61ec2019-04-10 11:51:58 -0600186 if (two_channels && tWTR[0] != tWTR[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200187 printf("/* tWTR mismatch: %d, %d */\n", tWTR[0], tWTR[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600188 print_time("tWTR", tWTR[channel], tCK);
Jacob Garber94d61ec2019-04-10 11:51:58 -0600189 if (two_channels && tCKE[0] != tCKE[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200190 printf("/* tCKE mismatch: %d, %d */\n", tCKE[0], tCKE[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600191 print_time("tCKE", tCKE[channel], tCK);
Jacob Garber94d61ec2019-04-10 11:51:58 -0600192 if (two_channels && tRTP[0] != tRTP[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200193 printf("/* tRTP mismatch: %d, %d */\n", tRTP[0], tRTP[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600194 print_time("tRTP", tRTP[channel], tCK);
Jacob Garber94d61ec2019-04-10 11:51:58 -0600195 if (two_channels && tRRD[0] != tRRD[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200196 printf("/* tRRD mismatch: %d, %d */\n", tRRD[0], tRRD[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600197 print_time("tRRD", tRRD[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200198
Jacob Garber94d61ec2019-04-10 11:51:58 -0600199 if (two_channels && tRAS[0] != tRAS[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200200 printf("/* tRAS mismatch: %d, %d */\n", tRAS[0], tRAS[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600201 print_time("tRAS", tRAS[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200202
Jacob Garber94d61ec2019-04-10 11:51:58 -0600203 if (two_channels && tCWL[0] != tCWL[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200204 printf("/* tCWL mismatch: %d, %d */\n", tCWL[0], tCWL[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600205 print_time("tCWL", tCWL[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200206
Jacob Garber94d61ec2019-04-10 11:51:58 -0600207 if (two_channels && tRP[0] != tRP[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200208 printf("/* tRP mismatch: %d, %d */\n", tRP[0], tRP[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600209 print_time("tRP", tRP[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200210
Jacob Garber94d61ec2019-04-10 11:51:58 -0600211 if (two_channels && tRCD[0] != tRCD[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200212 printf("/* tRCD mismatch: %d, %d */\n", tRCD[0], tRCD[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600213 print_time("tRCD", tRCD[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200214
Jacob Garber94d61ec2019-04-10 11:51:58 -0600215 if (two_channels && tXPDLL[0] != tXPDLL[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200216 printf("/* tXPDLL mismatch: %d, %d */\n", tXPDLL[0], tXPDLL[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600217 print_time("tXPDLL", tXPDLL[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200218
Jacob Garber94d61ec2019-04-10 11:51:58 -0600219 if (two_channels && tXP[0] != tXP[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200220 printf("/* tXP mismatch: %d, %d */\n", tXP[0], tXP[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600221 print_time("tXP", tXP[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200222
Jacob Garber94d61ec2019-04-10 11:51:58 -0600223 if (two_channels && tAONPD[0] != tAONPD[1])
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200224 printf("/* tAONPD mismatch: %d, %d */\n", tAONPD[0], tAONPD[1]);
Jacob Garbera7544072019-05-09 14:24:02 -0600225 print_time("tAONPD", tAONPD[channel], tCK);
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200226
227 reg = read_mchbar32(0x4298);
228 if (reg != read_mchbar32(0x4698) && two_channels)
229 printf("/* 4298 mismatch: %d, %d */\n", reg,
230 read_mchbar32(0x4698));
231
232 tREFI = reg & 0xffff;
233 print_time("tREFI", tREFI, tCK);
234 if ((tREFI * 9 / 1024) != (reg >> 25))
235 printf("/* tREFI mismatch: %d, %d */\n", tREFI * 9 / 1024,
236 (reg >> 25));
237 tRFC = (reg >> 16) & 0x1ff;
238 print_time("tRFC", tRFC, tCK);
239
240 reg = read_mchbar32(0x42a4);
241 if (reg != read_mchbar32(0x46a4) && two_channels)
242 printf("/* 42a4 mismatch: %d, %d */\n", reg,
243 read_mchbar32(0x46a4));
244
245 print_time("tMOD", 8 + ((reg >> 28) & 0xf), tCK);
246
247 tXSOffset = 512 - ((reg >> 16) & 0x3ff);
248 print_time("tXSOffset", tXSOffset, tCK);
249 if (tXSOffset != ((reg >> 12) & 0xf))
250 printf("/* tXSOffset mismatch: %d, %d */\n",
251 tXSOffset, (reg >> 12) & 0xf);
252 if (512 != (reg & 0xfff))
253 printf("/* tDLLK mismatch: %d, %d */\n", 512, reg & 0xfff);
254
255 reg = read_mchbar32(0x5064);
256 printf(".reg5064b0 = 0x%x,\n", reg & 0xfff);
257 if ((reg >> 12) != 0x73)
258 printf("/* mismatch 0x%x, 0x73. */\n", reg << 12);
259
260 unsigned int ch0size, ch1size;
261
262 switch (read_mchbar32(0x5000)) {
263 case 0x24:
264 reg = read_mchbar32(0x5014);
265 ch1size = reg >> 24;
266 if (((reg >> 16) & 0xff) != 2 * ch1size)
267 printf("/* ch1size mismatch: %d, %d*/\n",
268 2 * ch1size, ((ch1size >> 16) & 0xff));
269 printf(".channel_size_mb = { ?, %d },\n", ch1size * 256);
270 break;
271 case 0x21:
272 reg = read_mchbar32(0x5014);
273 ch0size = reg >> 24;
274 if (((reg >> 16) & 0xff) != 2 * ch0size)
275 printf("/* ch0size mismatch: %d, %d*/\n",
276 2 * ch0size, ((ch0size >> 16) & 0xff));
277 printf(".channel_size_mb = { %d, ? },\n", ch0size * 256);
278 break;
279 }
280
281 for (channel = 0; channel < 2; channel++) {
282 reg = mad_dimm[channel];
283 int swap = (reg >> 16) & 1;
284 slots[channel][swap].size_mb = (reg & 0xff) * 256;
285 slots[channel][swap].ranks = 1 + ((reg >> 17) & 1);
286 slots[channel][swap].width = 8 + 8 * ((reg >> 19) & 1);
287 slots[channel][!swap].size_mb = ((reg >> 8) & 0xff) * 256;
288 slots[channel][!swap].ranks = 1 + ((reg >> 18) & 1);
289 slots[channel][!swap].width = 8 + 8 * ((reg >> 20) & 1);
290 }
291 /* Undetermined: rank mirror, other modes, asr, ext_temp. */
292 memset(spd, 0, sizeof(spd));
293 for (channel = 0; channel < 2; channel++)
294 for (slot = 0; slot < 2; slot++)
295 if (slots[channel][slot].size_mb) {
296 printf("/* CH%dS%d: %d MiB */\n", channel,
297 slot, slots[channel][slot].size_mb);
298 }
299 for (channel = 0; channel < 2; channel++)
300 for (slot = 0; slot < 2; slot++)
301 if (slots[channel][slot].size_mb) {
302 int capacity_shift;
303 unsigned int ras, rc, rfc, faw;
304 u16 crc;
305 capacity_shift =
306 ffs(slots[channel][slot].size_mb *
307 slots[channel][slot].width /
308 (slots[channel][slot].ranks * 64)) - 1 -
309 5;
310
311 spd[channel][slot][0] = 0x92;
312 spd[channel][slot][1] = 0x11;
313 spd[channel][slot][2] = 0xb; /* DDR3 */
314 spd[channel][slot][3] = 3; /* SODIMM, should we use another type for desktop? */
315 spd[channel][slot][4] = capacity_shift | 0; /* 8 Banks. */
316 spd[channel][slot][5] = 0; /* FIXME */
317 spd[channel][slot][6] = 0; /* FIXME */
318 spd[channel][slot][7] =
319 ((slots[channel][slot].ranks -
320 1) << 3) | (ffs(slots[channel][slot].
321 width) - 1 - 2);
322 spd[channel][slot][8] = 3; /* Bus width 64b. No ECC yet. */
323 spd[channel][slot][9] = 0x52; /* 2.5ps. FIXME: choose dynamically if needed. */
324 spd[channel][slot][10] = 0x01;
325 spd[channel][slot][11] = 0x08; /* 1/8 ns. FIXME: choose dynamically if needed. */
326 spd[channel][slot][12] = make_spd_time(1, tCK);
327 spd[channel][slot][13] = 0;
328 spd[channel][slot][14] =
329 (1 << (CAS - 4)) & 0xff;
330 spd[channel][slot][15] = (1 << (CAS - 4)) >> 8;
331 spd[channel][slot][16] =
332 make_spd_time(CAS, tCK);
333 spd[channel][slot][17] =
334 make_spd_time(tWR, tCK);
335 spd[channel][slot][18] =
336 make_spd_time(tRCD[channel], tCK);
337 spd[channel][slot][19] =
338 make_spd_time(tRRD[channel], tCK);
339 spd[channel][slot][20] =
340 make_spd_time(tRP[channel], tCK);
341 ras = make_spd_time(tRAS[channel], tCK);
342 rc = 0x181; /* FIXME: should be make_spd_time(tRC, tCK). */
343 spd[channel][slot][22] = ras;
344 spd[channel][slot][23] = rc;
345 spd[channel][slot][21] =
346 ((ras >> 8) & 0xf) | ((rc >> 4) & 0xf0);
347 rfc = make_spd_time(tRFC, tCK);
348 spd[channel][slot][24] = rfc;
349 spd[channel][slot][25] = rfc >> 8;
350 spd[channel][slot][26] =
351 make_spd_time(tWTR[channel], tCK);
352 spd[channel][slot][27] =
353 make_spd_time(tRTP[channel], tCK);
354 faw = make_spd_time(tFAW[channel], tCK);
355 spd[channel][slot][28] = faw >> 8;
356 spd[channel][slot][29] = faw;
357 spd[channel][slot][30] = 0; /* FIXME */
358 spd[channel][slot][31] = 0; /* FIXME */
359 spd[channel][slot][32] = 0; /* FIXME */
360 spd[channel][slot][33] = 0; /* FIXME */
361 spd[channel][slot][62] = 0x65; /* Reference card F. FIXME */
362 spd[channel][slot][63] = 0; /* FIXME */
363 crc = spd_ddr3_calc_crc(spd[channel][slot]);
364 spd[channel][slot][126] = crc;
365 spd[channel][slot][127] = crc >> 8;
366 }
367
368 printf("/* SPD matching current mode: */\n");
369
Stefan Tauner572f0742016-05-05 17:29:39 +0200370 FILE *dump_spd = NULL;
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +0200371
372 if (dump_spd_file) {
373 dump_spd = fopen (dump_spd_file, "wb");
374 if (!dump_spd) {
375 fprintf (stderr, "Couldn't open file %s: %s\n", dump_spd_file,
376 strerror (errno));
377 exit (1);
378 }
379 }
380
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200381 for (channel = 0; channel < 2; channel++)
382 for (slot = 0; slot < 2; slot++)
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +0200383 {
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200384 if (slots[channel][slot].size_mb) {
385 int i;
386
387 printf("/* CH%dS%d */\n", channel, slot);
388
389 for (i = 0; i < 256; i++) {
390 if ((i & 0xf) == 0x0)
391 printf("%02x: ", i);
392 printf("%02x ", spd[channel][slot][i]);
393 if ((i & 0xf) == 0xf)
394 printf("\n");
395 }
396 printf("\n");
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +0200397
398 if (dump_spd) {
399 fwrite(spd[channel][slot], 1, 256, dump_spd);
400 }
401 } else {
402 if (dump_spd) {
403 char zero[256];
404 memset (zero, 0, 256);
405 fwrite(zero, 1, 256, dump_spd);
406 }
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200407 }
Vladimir Serbinenkofb69a692015-10-10 13:20:32 +0200408 }
409 if (dump_spd) {
410 fclose (dump_spd);
411 }
Vladimir Serbinenko44bc11c2014-08-16 19:14:02 +0200412
413}