blob: 2de7734c177b6a060d9f43a3b1ceb8d942f6cac9 [file] [log] [blame]
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011-2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -050015 */
16
17#include "early_vx900.h"
18#include "raminit.h"
19#include <arch/io.h>
20#include <arch/io.h>
21#include <console/console.h>
22#include <device/pci_ids.h>
23#include <delay.h>
24#include <lib.h>
25#include <string.h>
26
27/**
28 * @file raminit_ddr3.c
29 *
30 * \brief DDR3 initialization for VIA VX900 chipset
31 *
32 * Rather than explain the DDR3 init algorithm, it is better to focus on what
33 * works and what doesn't. Familiarity with the DDR3 spec does not hurt.
34 *
35 * 1 DIMMs and 2 DIMMs with one rank each works.
36 * 1 rank DIMM with 2 rank DIMM works, but the odd ranks are disabled.
37 * (2) 2-rank DIMMs will not work.
38 *
39 * It is not yet clear if odd ranks do not work because of faulty timing
40 * calibration, or a misconfiguration of the MCU. I have seen this with DIMMS
41 * which mirror pins on the odd rank. That could also be the issue.
42 *
43 * The capture window is not calibrated, but preset. Whether that preset is
44 * universal or frequency dependent, and whether it is board-specific or not is
45 * not yet clear. @see vx900_dram_calibrate_recieve_delays().
46 *
47 * 4GBit and 8GBit modules may not work. This is untested. Modules with 11
48 * column address bits are not tested. @see vx900_dram_map_row_col_bank()
49 *
50 * Everything else should be in a more or less usable state. FIXME s are placed
51 * all over as a reminder that either something really needs fixing, or as a
52 * reminder to double-check.
53 */
54
55/* Map BA0 <-> A17, BA1 <-> A18 */
56/* Map BA2 <-> A19, RA0/RA1 must not overlap BA[0:2] */
57#define VX900_MRS_MA_MAP 0x4b33 /* MA Pin Mapping for MRS commands */
58#define VX900_CALIB_MA_MAP 0x5911 /* MA Pin mapping for calibrations */
59
60/*
61 * Registers 0x78 -> 0x7f contain the calibration settings for DRAM IO timing
62 * The dataset in these registers is selected from 0x70.
63 * Once the correct dataset is selected the delays can be altered.
64 * delay_type refers to TxDQS, TxDQ, RxDQS, or RxCR
65 * bound refers to either manual, average, upper bound, or lower bound
66 */
67#define CALIB_TxDQS 0
68#define CALIB_TxDQ 1
69#define CALIB_RxDQS 2
70#define CALIB_RxDQ_CR 3
71
72#define CALIB_AVERAGE 0
73#define CALIB_LOWER 1
74#define CALIB_UPPER 2
75#define CALIB_MANUAL 4 /* We want this & 3 to overflow to 0 */
76
77static void vx900_delay_calib_mode_select(u8 delay_type, u8 bound)
78{
79 /* Which calibration setting */
80 u8 reg8 = (delay_type & 0x03) << 2;
81 /* Upper, lower, average, or manual setting */
82 reg8 |= (bound & 0x03);
83 pci_write_config8(MCU, 0x70, reg8);
84}
85
86/*
87 * The vendor BIOS does something similar to vx900_delay_calib_mode_select(),
88 * then reads or write a byte, and repeats the process for all 8 bytes. This is
89 * annoyingly inefficient, and we can achieve the same result in a much more
90 * elegant manner.
91 */
92static void vx900_read_0x78_0x7f(timing_dly dly)
93{
94 *((u32 *) (&(dly[0]))) = pci_read_config32(MCU, 0x78);
95 *((u32 *) (&(dly[4]))) = pci_read_config32(MCU, 0x7c);
96}
97
98static void vx900_write_0x78_0x7f(const timing_dly dly)
99{
100 pci_write_config32(MCU, 0x78, *((u32 *) (&(dly[0]))));
101 pci_write_config32(MCU, 0x7c, *((u32 *) (&(dly[4]))));
102}
103
104static void vx900_read_delay_range(delay_range * d_range, u8 mode)
105{
106 vx900_delay_calib_mode_select(mode, CALIB_LOWER);
107 vx900_read_0x78_0x7f(d_range->low);
108 vx900_delay_calib_mode_select(mode, CALIB_AVERAGE);
109 vx900_read_0x78_0x7f(d_range->avg);
110 vx900_delay_calib_mode_select(mode, CALIB_UPPER);
111 vx900_read_0x78_0x7f(d_range->high);
112}
113
114static void dump_delay(const timing_dly dly)
115{
116 u8 i;
117 for (i = 0; i < 8; i++) {
118 printram(" %.2x", dly[i]);
119 }
120 printram("\n");
121}
122
123static void dump_delay_range(const delay_range d_range)
124{
125 printram("Lower limit: ");
126 dump_delay(d_range.low);
127 printram("Average: ");
128 dump_delay(d_range.avg);
129 printram("Upper limit: ");
130 dump_delay(d_range.high);
131}
132
133/*
134 * These are some "safe" values that can be used for memory initialization.
135 * Some will stay untouched, and others will be overwritten later on
136 */
137static pci_reg8 mcu_init_config[] = {
138 {0x40, 0x01}, /* Virtual rank 0 ending address = 64M - 1 */
139 {0x41, 0x00}, {0x42, 0x00}, {0x43, 0x00}, /* Virtual Ranks ending */
140 {0x48, 0x00}, /* Virtual rank 0 starting address = 0 */
141 {0x49, 0x00}, {0x4a, 0x00}, {0x4b, 0x00}, /* Virtual Ranks beginning */
142 {0x50, 0xd8}, /* Set ranks 0-3 to 11 col bits, 16 row bits */
143 /* Disable all virtual ranks */
144 {0x54, 0x00}, {0x55, 0x00}, {0x56, 0x00}, {0x57, 0x00},
145 /* Disable rank interleaving in ranks 0-3 */
146 {0x58, 0x00}, {0x59, 0x00}, {0x5a, 0x00}, {0x5b, 0x00},
147 {0x6c, 0xA0}, /* Memory type: DDR3, VDIMM: 1.5V, 64-bit DRAM */
148 {0xc4, 0x80}, /* Enable 8 memory banks */
149 {0xc6, 0x80}, /* Minimum latency from self-refresh. Bit [7] must be 1 */
150 /* FIXME: do it here or in Final config? */
151 {0xc8, 0x80}, /* Enable automatic triggering of short ZQ calibration */
152 {0x99, 0xf0}, /* Power Management and Bypass Reorder Queue */
153 /* Enable differential DQS; MODT assertion values suggested in DS */
154 {0x9e, 0xa1}, {0x9f, 0x51},
155 /* DQ/DQM Duty Control - Do not put any extra delays */
156 {0xe9, 0x00}, {0xea, 0x00}, {0xeb, 0x00}, {0xec, 0x00},
157 {0xed, 0x00}, {0xee, 0x00}, {0xef, 0x00},
158 {0xfc, 0x00}, {0xfd, 0x00}, {0xfe, 0x00}, {0xff, 0x00},
159 /* The following parameters we may or may not change */
160 {0x61, 0x2e}, /* DRAMC Pipeline Control */
161 {0x77, 0x10}, /* MDQS Output Control */
162
163 /* The following are parameters we'll most likely never change again */
164 {0x60, 0xf4}, /* DRAM Pipeline Turn-Around Setting */
165 {0x65, 0x49}, /* DRAM Arbitration Bandwidth Timer - I */
166 {0x66, 0x80}, /* DRAM Queue / Arbitration */
167 {0x69, 0xc6}, /* Bank Control: 8 banks, high priority refresh */
168 {0x6a, 0xfc}, /* DRAMC Request Reorder Control */
169 {0x6e, 0x38}, /* Burst lenght: 8, burst-chop: enable */
170 {0x73, 0x04}, /* Close All Pages Threshold */
171
172 /* The following need to be dynamically asserted */
173 /* See: check_special_registers.c */
174 {0x74, 0xa0}, /* Yes, same 0x74; add one more T */
175 {0x76, 0x60}, /* Write Data Phase Control */
176
177};
178
179/*
180 * This table keeps the driving strength control setting that we can safely use
181 * during initialization. This settings come in part from SerialICE, and in part
182 * from code provided by VIA.
183 */
184static pci_reg8 mcu_drv_ctrl_config[] = {
185 {0xd3, 0x03}, /* Enable auto-compensation circuit for ODT strength */
186 {0xd4, 0x80}, /* Set internal ODT to dynamically turn on or off */
187 {0xd6, 0x20}, /* Enable strong driving for MA and DRAM commands */
188 {0xd0, 0x88}, /* (ODT) Strength ?has effect? */
Martin Roth0cd338e2016-07-29 14:07:30 -0600189 {0xe0, 0x88}, /* DRAM Driving - Group DQS (MDQS) */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500190 {0xe1, 0x00}, /* Disable offset mode for driving strength control */
Martin Roth0cd338e2016-07-29 14:07:30 -0600191 {0xe2, 0x88}, /* DRAM Driving - Group DQ (MD, MDQM) */
192 {0xe4, 0xcc}, /* DRAM Driving - Group CSA (MCS, MCKE, MODT) */
193 {0xe8, 0x88}, /* DRAM Driving - Group MA (MA, MBA, MSRAS, MSCAS, MSWE) */
194 {0xe6, 0xff}, /* DRAM Driving - Group DCLK0 (DCLK[2:0] for DIMM0) */
195 {0xe7, 0xff}, /* DRAM Driving - Group DCLK1 (DCLK[5:3] for DIMM1) */
196 {0xe4, 0xcc}, /* DRAM Driving - Group CSA (MCS, MCKE, MODT) */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500197 {0x91, 0x08}, /* MCLKO Output Phase Delay - I */
198 {0x92, 0x08}, /* MCLKO Output Phase Delay - II */
199 {0x93, 0x16}, /* CS/CKE Output Phase Delay */
200 {0x95, 0x16}, /* SCMD/MA Output Phase Delay */
201 {0x9b, 0x3f}, /* Memory Clock Output Enable */
202};
203
204static void vx900_dram_set_ma_pin_map(u16 map)
205{
206 pci_write_config16(MCU, 0x52, map);
207}
208
209/*
210 * FIXME: This function is a complete waste of space. All we really need is a
211 * MA MAP table based on either row address bits or column address bits.
212 * The problem is, I do not know if this mapping is applied during the column
213 * access or during the row access. At least the religiously verbose output
214 * makes pretty console output.
215 */
216static void vx900_dram_map_pins(u8 ba0, u8 ba1, u8 ba2, u8 ra0, u8 ra1)
217{
218 u16 map = 0;
219
220 printram("Mapping address pins to DRAM pins:\n");
221 printram(" BA0 -> A%u\n", ba0);
222 printram(" BA1 -> A%u\n", ba1);
223 printram(" BA2 -> A%u\n", ba2);
224 printram(" RA0 -> A%u\n", ra0);
225 printram(" RA1 -> A%u\n", ra1);
226 /* Make sure BA2 is enabled */
227 map |= (1 << 11);
228
229 /*
230 * Find RA1 (15:14)
231 * 00: A14
232 * 01: A16
233 * 10: A18
234 * 11: A20
235 */
236 if ((ra1 & 0x01) || (ra1 < 14) || (ra1 > 20)) {
237 printram("Illegal mapping RA1 -> A%u\n", ra1);
238 return;
239 }
240 map |= (((ra1 - 14) >> 1) & 0x03) << 14;
241
242 /*
243 * Find RA0 (13:12)
244 * 00: A15
245 * 01: A17
246 * 10: A19
247 * 11: A21
248 */
249 if ((!(ra0 & 0x01)) || (ra0 < 15) || (ra0 > 21)) {
250 printram("Illegal mapping RA0 -> A%u\n", ra0);
251 return;
252 }
253 map |= (((ra0 - 15) >> 1) & 0x03) << 12;
254
255 /*
256 * Find BA2 (10:8)
257 * x00: A14
258 * x01: A15
259 * x10: A18
260 * x11: A19
261 */
262 switch (ba2) {
263 case 14:
264 map |= (0 << 8);
265 break;
266 case 15:
267 map |= (1 << 8);
268 break;
269 case 18:
270 map |= (2 << 8);
271 break;
272 case 19:
273 map |= (3 << 8);
274 break;
275 default:
276 printram("Illegal mapping BA2 -> A%u\n", ba2);
277 break;
278 }
279
280 /*
281 * Find BA1 (6:4)
282 * 000: A12
283 * 001: A14
284 * 010: A16
285 * 011: A18
286 * 1xx: A20
287 */
288 if (((ba1 & 0x01)) || (ba1 < 12) || (ba1 > 20)) {
289 printram("Illegal mapping BA1 -> A%u\n", ba1);
290 return;
291 }
292 map |= (((ba1 - 12) >> 1) & 0x07) << 4;
293
294 /*
295 * Find BA0 (2:0)
296 * 000: A11
297 * 001: A13
298 * 010: A15
299 * 011: A17
300 * 1xx: A19
301 */
302 if ((!(ba0 & 0x01)) || (ba0 < 11) || (ba0 > 19)) {
303 printram("Illegal mapping BA0 -> A%u\n", ba0);
304 return;
305 }
306 map |= (((ba0 - 11) >> 1) & 0x07) << 0;
307
308 printram("Setting map mask (rx52) to %.4x\n", map);
309 vx900_dram_set_ma_pin_map(map);
310}
311
312static void vx900_dram_write_init_config(void)
313{
314 /* Keep our RAM space free of legacy stuff */
315 vx900_disable_legacy_rom_shadow();
316
317 /* Now worry about the real RAM init */
318 size_t i;
319 for (i = 0; i < (sizeof(mcu_init_config) / sizeof(pci_reg8)); i++) {
320 pci_write_config8(MCU, mcu_init_config[i].addr,
321 mcu_init_config[i].val);
322 }
323 vx900_dram_set_ma_pin_map(VX900_CALIB_MA_MAP);
324
325 /* FIXME: Slowing stuff down. Does this really help? */
326
327 /* Fast cycle control for CPU-to-DRAM Read Cycle 0:Disabled.
328 * This CPU bus controller will wait for all data */
Elyes HAOUASe7aeb2f2016-10-05 21:02:23 +0200329
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500330 /* Memory to CPU bus Controller Conversion Mode 1: Synchronous mode */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500331}
332
333static void dram_find_spds_ddr3(const dimm_layout * addr, dimm_info * dimm)
334{
335 size_t i = 0;
336 int dimms = 0;
337 do {
338 spd_raw_data spd;
339 spd_read(addr->spd_addr[i], spd);
340 spd_decode_ddr3(&dimm->dimm[i], spd);
341 if (dimm->dimm[i].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
342 continue;
343 dimms++;
344 dram_print_spd_ddr3(&dimm->dimm[i]);
345 } while (addr->spd_addr[++i] != SPD_END_LIST
346 && i < VX900_MAX_DIMM_SLOTS);
347
348 if (!dimms)
349 die("No DIMMs were found");
350}
351
352static void dram_find_common_params(const dimm_info * dimms,
353 ramctr_timing * ctrl)
354{
355 size_t i, valid_dimms;
356 memset(ctrl, 0, sizeof(ramctr_timing));
357 ctrl->cas_supported = 0xff;
358 valid_dimms = 0;
359 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i++) {
360 const dimm_attr *dimm = &dimms->dimm[i];
361 if (dimm->dram_type == SPD_MEMORY_TYPE_UNDEFINED)
362 continue;
363 valid_dimms++;
364
365 if (valid_dimms == 1) {
366 /* First DIMM defines the type of DIMM */
367 ctrl->dram_type = dimm->dram_type;
Vladimir Serbinenkodaf76802014-12-07 13:58:15 +0100368 ctrl->dimm_type = dimm->dimm_type;
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500369 } else {
370 /* Check if we have mismatched DIMMs */
Vladimir Serbinenkodaf76802014-12-07 13:58:15 +0100371 if (ctrl->dram_type != dimm->dram_type
372 || ctrl->dimm_type != dimm->dimm_type)
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500373 die("Mismatched DIMM Types");
374 }
375 /* Find all possible CAS combinations */
376 ctrl->cas_supported &= dimm->cas_supported;
377
378 /* Find the smallest common latencies supported by all DIMMs */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -0500379 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
380 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
381 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
382 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
383 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
384 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
385 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
386 ctrl->tRC = MAX(ctrl->tRC, dimm->tRC);
387 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
388 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
389 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
390 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500391
392 }
393
394 ctrl->n_dimms = valid_dimms;
395 if (!ctrl->cas_supported)
396 die("Unsupported DIMM combination. "
397 "DIMMS do not support common CAS latency");
398 if (!valid_dimms)
399 die("No valid DIMMs found");
400}
401
402static void vx900_dram_phys_bank_range(const dimm_info * dimms,
403 rank_layout * ranks)
404{
405 size_t i;
406 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i++) {
407 if (dimms->dimm[i].dram_type == SPD_MEMORY_TYPE_UNDEFINED)
408 continue;
409 u8 nranks = dimms->dimm[i].ranks;
410 /* Make sure we save the flags */
411 ranks->flags[i * 2 + 1] = ranks->flags[i * 2] =
412 dimms->dimm[i].flags;
413 /* Only Rank1 has a mirrored pin mapping */
414 ranks->flags[i * 2].pins_mirrored = 0;
415 if (nranks > 2)
416 die("Found DIMM with more than two ranks, which is not "
417 "supported by this chipset");
418 u32 size = dimms->dimm[i].size_mb;
419 if (nranks == 2) {
420 /* Each rank holds half the capacity of the DIMM */
421 size >>= 1;
422 ranks->phys_rank_size_mb[i << 1] = size;
423 ranks->phys_rank_size_mb[(i << 1) | 1] = size;
424 } else {
425 /* Otherwise, everything is held in the first bank */
426 ranks->phys_rank_size_mb[i << 1] = size;
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000427 ranks->phys_rank_size_mb[(i << 1) | 1] = 0;
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500428 }
429 }
430}
431
432#define ODT_R0 0
433#define ODT_R1 1
434#define ODT_R2 2
435#define ODT_R3 3
436/*
437 * This is the table that tells us which MODT pin to map to which rank.
438 *
439 * This table is taken from code provided by VIA, but no explanation was
440 * provided as to why it is done this way. It may be possible that this table is
441 * not suitable for the way we map ranks later on.
442 */
443static const u8 odt_lookup_table[][2] = {
444 /* RankMAP Rank 3 Rank 2 Rank 1 Rank 0 */
445 {0x01, (ODT_R3 << 6) | (ODT_R2 << 4) | (ODT_R1 << 2) | (ODT_R0 << 0)},
446 {0x03, (ODT_R3 << 6) | (ODT_R2 << 4) | (ODT_R0 << 2) | (ODT_R1 << 0)},
447 {0x04, (ODT_R3 << 6) | (ODT_R2 << 4) | (ODT_R1 << 2) | (ODT_R0 << 0)},
448 {0x05, (ODT_R3 << 6) | (ODT_R0 << 4) | (ODT_R1 << 2) | (ODT_R2 << 0)},
449 {0x07, (ODT_R3 << 6) | (ODT_R0 << 4) | (ODT_R2 << 2) | (ODT_R2 << 0)},
450 {0x0c, (ODT_R2 << 6) | (ODT_R3 << 4) | (ODT_R1 << 2) | (ODT_R0 << 0)},
451 {0x0d, (ODT_R0 << 6) | (ODT_R0 << 4) | (ODT_R1 << 2) | (ODT_R2 << 0)},
452 {0x0f, (ODT_R0 << 6) | (ODT_R0 << 4) | (ODT_R2 << 2) | (ODT_R2 << 0)},
453 {0, 0},
454};
455
456static void vx900_dram_driving_ctrl(const dimm_info * dimm)
457{
458 size_t i, ndimms;
459 u8 reg8, regxd5, rank_mask;
460
461 rank_mask = 0;
462 /* For ODT range selection, datasheet recommends
463 * when 1 DIMM present: 60 Ohm
464 * when 2 DIMMs present: 120 Ohm */
465 ndimms = 0;
466 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i++) {
467 if (dimm->dimm[i].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
468 continue;
469 ndimms++;
470 rank_mask |= (1 << (i * 2));
471 if (dimm->dimm[i].ranks > 1)
472 rank_mask |= (2 << (i * 2));
473 }
474 /* ODT strength and MD/MDQM/MDQS driving strength */
475 if (ndimms > 1) {
476 /* Enable 1 ODT block (120 Ohm ODT) */
477 regxd5 = 0 << 2;
478 /* Enable strong driving for MD/MDQM/MDQS */
479 regxd5 |= (1 << 7);
480 } else {
481 /* Enable 2 ODT blocks (60 Ohm ODT) */
482 regxd5 = 1 << 2;
483 /* Leave MD/MDQM/MDQS driving weak */
484 }
485 pci_write_config8(MCU, 0xd5, regxd5);
486
487 /* Enable strong CLK driving for DIMMs with more than one rank */
488 if (dimm->dimm[0].ranks > 1)
489 pci_mod_config8(MCU, 0xd6, 0, (1 << 7));
490 if (dimm->dimm[1].ranks > 1)
491 pci_mod_config8(MCU, 0xd6, 0, (1 << 6));
492
493 /* DRAM ODT Lookup Table */
494 for (i = 0;; i++) {
495 if (odt_lookup_table[i][0] == 0) {
496 printram("No ODT entry for rank mask %x\n", rank_mask);
497 die("Aborting");
498 }
499 if (odt_lookup_table[i][0] != rank_mask)
500 continue;
501
502 reg8 = odt_lookup_table[i][1];
503 break;
504 }
505
506 printram("Mapping rank mask %x to ODT entry %.2x\n", rank_mask, reg8);
507 pci_write_config8(MCU, 0x9c, reg8);
508
509 for (i = 0; i < (sizeof(mcu_drv_ctrl_config) / sizeof(pci_reg8)); i++) {
510 pci_write_config8(MCU, mcu_drv_ctrl_config[i].addr,
511 mcu_drv_ctrl_config[i].val);
512 }
513}
514
515static void vx900_pr_map_all_vr3(void)
516{
517 /* Enable all ranks and set them to VR3 */
518 pci_write_config16(MCU, 0x54, 0xbbbb);
519}
520
521/* Map physical rank pr to virtual rank vr */
522static void vx900_map_pr_vr(u8 pr, u8 vr)
523{
524 u16 val;
525
526 pr &= 0x3;
527 vr &= 0x3;
528 /* Enable rank (bit [3], and set the VR number bits [1:0] */
529 val = 0x8 | vr;
530 /* Now move the value to the appropriate PR */
531 val <<= (pr * 4);
532 pci_mod_config16(MCU, 0x54, 0xf << (pr * 4), val);
533 printram("Mapping PR %u to VR %u\n", pr, vr);
534}
535
536static u8 vx900_get_CWL(u8 CAS)
537{
538 /* Get CWL based on CAS using the following rule:
539 * _________________________________________
540 * CAS: | 4T | 5T | 6T | 7T | 8T | 9T | 10T | 11T |
541 * CWL: | 5T | 5T | 5T | 6T | 6T | 7T | 7T | 8T |
542 */
543 static const u8 cas_cwl_map[] = { 5, 5, 5, 6, 6, 7, 7, 8 };
544 if (CAS > 11)
545 return 8;
546 return cas_cwl_map[CAS - 4];
547}
548
549/*
550 * Here we are calculating latencies, and writing them to the appropriate
551 * registers. Note that some registers do not take latencies from 0 = 0T,
552 * 1 = 1T, so each register gets its own math formula.
553 */
554static void vx900_dram_timing(ramctr_timing * ctrl)
555{
556 u8 reg8, val, tFAW, tRRD;
557 u32 val32;
558
559 /* Maximum supported DDR3 frequency is 533MHz (DDR3 1066) so make sure
560 * we cap it if we have faster DIMMs.
561 * Then, align it to the closest JEDEC standard frequency */
562 if (ctrl->tCK <= TCK_533MHZ) {
563 ctrl->tCK = TCK_533MHZ;
564 } else if (ctrl->tCK <= TCK_400MHZ) {
565 ctrl->tCK = TCK_400MHZ;
566 } else if (ctrl->tCK <= TCK_333MHZ) {
567 ctrl->tCK = TCK_333MHZ;
568 } else {
569 ctrl->tCK = TCK_266MHZ;
570 }
571
572 val32 = (1000 << 8) / ctrl->tCK;
573 printram("Selected DRAM frequency: %u MHz\n", val32);
574
575 /* Find CAS and CWL latencies */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000576 val = CEIL_DIV(ctrl->tAA, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500577 printram("Minimum CAS latency : %uT\n", val);
578 /* Find lowest supported CAS latency that satisfies the minimum value */
579 while (!((ctrl->cas_supported >> (val - 4)) & 1)
580 && (ctrl->cas_supported >> (val - 4))) {
581 val++;
582 }
583 /* Is CAS supported */
584 if (!(ctrl->cas_supported & (1 << (val - 4))))
585 printram("CAS not supported\n");
586 printram("Selected CAS latency : %uT\n", val);
587 ctrl->CAS = val;
588 ctrl->CWL = vx900_get_CWL(ctrl->CAS);
589 printram("Selected CWL latency : %uT\n", ctrl->CWL);
590 /* Write CAS and CWL */
591 reg8 = (((ctrl->CWL - 4) & 0x07) << 4) | ((ctrl->CAS - 4) & 0x07);
592 pci_write_config8(MCU, 0xc0, reg8);
593
594 /* Find tRCD */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000595 val = CEIL_DIV(ctrl->tRCD, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500596 printram("Selected tRCD : %uT\n", val);
597 reg8 = ((val - 4) & 0x7) << 4;
598 /* Find tRP */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000599 val = CEIL_DIV(ctrl->tRP, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500600 printram("Selected tRP : %uT\n", val);
601 reg8 |= ((val - 4) & 0x7);
602 pci_write_config8(MCU, 0xc1, reg8);
603
604 /* Find tRAS */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000605 val = CEIL_DIV(ctrl->tRAS, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500606 printram("Selected tRAS : %uT\n", val);
607 reg8 = ((val - 15) & 0x7) << 4;
608 /* Find tWR */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000609 ctrl->WR = CEIL_DIV(ctrl->tWR, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500610 printram("Selected tWR : %uT\n", ctrl->WR);
611 reg8 |= ((ctrl->WR - 4) & 0x7);
612 pci_write_config8(MCU, 0xc2, reg8);
613
614 /* Find tFAW */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000615 tFAW = CEIL_DIV(ctrl->tFAW, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500616 printram("Selected tFAW : %uT\n", tFAW);
617 /* Find tRRD */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000618 tRRD = CEIL_DIV(ctrl->tRRD, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500619 printram("Selected tRRD : %uT\n", tRRD);
620 val = tFAW - 4 * tRRD; /* number of cycles above 4*tRRD */
621 reg8 = ((val - 0) & 0x7) << 4;
622 reg8 |= ((tRRD - 2) & 0x7);
623 pci_write_config8(MCU, 0xc3, reg8);
624
625 /* Find tRTP */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000626 val = CEIL_DIV(ctrl->tRTP, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500627 printram("Selected tRTP : %uT\n", val);
628 reg8 = ((val & 0x3) << 4);
629 /* Find tWTR */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000630 val = CEIL_DIV(ctrl->tWTR, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500631 printram("Selected tWTR : %uT\n", val);
632 reg8 |= ((val - 2) & 0x7);
633 pci_mod_config8(MCU, 0xc4, 0x3f, reg8);
634
635 /* DRAM Timing for All Ranks - VI
636 * [7:6] CKE Assertion Minimum Pulse Width
637 * We probably don't want to mess with this just yet.
638 * [5:0] Refresh-to-Active or Refresh-to-Refresh (tRFC)
639 * tRFC = (30 + 2 * [5:0])T
640 * Since we previously set RxC4[7]
641 */
642 reg8 = pci_read_config8(MCU, 0xc5);
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000643 val = CEIL_DIV(ctrl->tRFC, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500644 printram("Minimum tRFC : %uT\n", val);
645 if (val < 30) {
646 val = 0;
647 } else {
648 val = (val - 30 + 1) / 2;
649 }
650 ;
651 printram("Selected tRFC : %uT\n", 30 + 2 * val);
652 reg8 |= (val & 0x3f);
653 pci_write_config8(MCU, 0xc5, reg8);
654
655 /* Where does this go??? */
Edward O'Callaghan7116ac82014-07-08 01:53:24 +1000656 val = CEIL_DIV(ctrl->tRC, ctrl->tCK);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500657 printram("Required tRC : %uT\n", val);
658}
659
660/* Program the DRAM frequency */
661static void vx900_dram_freq(ramctr_timing * ctrl)
662{
663 u8 val;
664
665 /* Step 1 - Reset the PLL */
666 pci_mod_config8(MCU, 0x90, 0x00, 0x0f);
667 /* Wait at least 10 ns; VIA code delays by 640us */
668 udelay(640);
669
670 /* Step 2 - Set target frequency */
671 if (ctrl->tCK <= TCK_533MHZ) {
672 val = 0x07;
673 ctrl->tCK = TCK_533MHZ;
674 } else if (ctrl->tCK <= TCK_400MHZ) {
675 val = 0x06;
676 ctrl->tCK = TCK_400MHZ;
677 } else if (ctrl->tCK <= TCK_333MHZ) {
678 val = 0x05;
679 ctrl->tCK = TCK_333MHZ;
680 } else { /*ctrl->tCK <= TCK_266MHZ */
681 val = 0x04;
682 ctrl->tCK = TCK_266MHZ;
683 }
684 /* Restart the PLL with the desired frequency */
685 pci_mod_config8(MCU, 0x90, 0x0f, val);
686
687 /* Step 3 - Wait for PLL to stabilize */
688 udelay(2000);
689
690 /* Step 4 - Reset the DLL - Clear [7,4] */
691 pci_mod_config8(MCU, 0x6b, 0x90, 0x00);
692 udelay(2000);
693
694 /* Step 5 - Enable the DLL - Set bits [7,4] to 01b */
695 pci_mod_config8(MCU, 0x6b, 0x00, 0x10);
696 udelay(2000);
697
698 /* Step 6 - Start DLL Calibration - Set bit [7] */
699 pci_mod_config8(MCU, 0x6b, 0x00, 0x80);
700 udelay(5);
701
702 /* Step 7 - Finish DLL Calibration - Clear bit [7] */
703 pci_mod_config8(MCU, 0x6b, 0x80, 0x00);
704
705 /* Step 8 - If we have registered DIMMs, we need to set bit[0] */
Patrick Rudolph6e53ae62017-01-31 19:43:17 +0100706 if (spd_dimm_is_registered_ddr3(ctrl->dimm_type)) {
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500707 printram("Enabling RDIMM support in memory controller\n");
708 pci_mod_config8(MCU, 0x6c, 0x00, 0x01);
709 }
710}
711
712/*
713 * The VX900 can send the MRS commands directly through hardware
714 * It does the MR2->MR3->MR1->MR0->LongZQ JEDEC dance
715 * The parameters that we don't worry about are extracted from the timing
716 * registers we have programmed earlier.
717 */
718static void vx900_dram_ddr3_do_hw_mrs(u8 ma_swap, u8 rtt_nom,
719 u8 ods, u8 rtt_wr, u8 srt, u8 asr)
720{
721 u16 reg16 = 0;
722
723 printram("Using Hardware method for DRAM MRS commands.\n");
724
725 reg16 |= ((rtt_wr & 0x03) << 12);
726 if (srt)
727 reg16 |= (1 << 9);
728 if (asr)
729 reg16 |= (1 << 8);
730 reg16 |= ((rtt_nom & 0x7) << 4);
731 reg16 |= ((ods & 0x03) << 2);
732 if (ma_swap)
733 reg16 |= (1 << 1);
734 reg16 |= (1 << 14);
735 reg16 |= (1 << 0); /* This is the trigger bit */
736 printram("Hw MRS set is 0x%4x\n", reg16);
737 pci_write_config16(MCU, 0xcc, reg16);
738 /* Wait for MRS commands to be sent */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200739 while (pci_read_config8(MCU, 0xcc) & 1);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500740}
741
742/*
743 * Translate the MRS command into an address on the CPU bus
744 *
745 * Take an MRS command (mrs_cmd_t) and translate it to a read address on the CPU
746 * bus. Thus, reading from the returned address, will issue the correct MRS
747 * command, assuming we are in MRS mode, of course.
748 *
749 * A read from the returned address will produce the correct MRS command
750 * provided the following conditions are met:
751 * - The MA pin mapping is set to VX900_MRS_MA_MAP
752 * - The memory controller's Fun3_RX6B[2:0] is set to 011b (MSR Enable)
753 */
754static u32 vx900_get_mrs_addr(mrs_cmd_t cmd)
755{
756 u32 addr = 0;
757 u8 mrs_type = (cmd >> 16) & 0x07;
758 /* MA[9:0] <-> A[12:3] */
759 addr |= ((cmd & 0x3ff) << 3);
760 /* MA10 <-> A20 */
761 addr |= (((cmd >> 10) & 0x1) << 20);
762 /* MA[12:11] <-> A[14:13] */
763 addr |= (((cmd >> 11) & 0x3) << 13);
764 /* BA[2:0] <-> A[19:17] */
765 addr |= mrs_type << 17;
766 return addr;
767}
768
769/*
770 * Here, we do the MR2->MR3->MR1->MR0->LongZQ JEDEC dance manually
771 *
772 * Why would we do this in software, when the VX900 can do it in hardware? The
773 * problem is the hardware sequence seems to be buggy on ranks with mirrored
774 * pins. Is this a hardware bug or a misconfigured MCU? No idea.
775 *
776 * To maintain API compatibility with the function that implements the hardware
777 * sequence, we don't ask for all parameters. To keep an overall cleaner code
778 * structure, we don't try to pass down all that information. Instead, we
779 * extract the extra parameters from the timing registers we have programmed
780 * earlier.
781 */
782static void vx900_dram_ddr3_do_sw_mrs(u8 ma_swap, enum ddr3_mr1_rtt_nom rtt_nom,
783 enum ddr3_mr1_ods ods,
784 enum ddr3_mr2_rttwr rtt_wr,
785 enum ddr3_mr2_srt_range srt,
786 enum ddr3_mr2_asr asr)
787{
788 mrs_cmd_t mrs;
789 u8 reg8, cas, cwl, twr;
790
791 printram("Using Software method for DRAM MRS commands.\n");
792
793 /* Get CAS, CWL, and tWR that we programmed earlier */
794 reg8 = pci_read_config8(MCU, 0xc0);
795 cas = (reg8 & 0x07) + 4;
796 cwl = ((reg8 >> 4) & 0x07) + 4;
797 reg8 = pci_read_config8(MCU, 0xc2);
798 twr = (reg8 & 0x07) + 4;
799
800 /* Step 06 - Set Fun3_RX6B[2:0] to 001b (NOP Command Enable). */
801 /* Was already done for us before calling us */
802
803 /* Step 07 - Read a double word from any address of the DIMM. */
804 /* Was already done for us before calling us */
805
806 /* Step 08 - Set Fun3_RX6B[2:0] to 011b (MSR Enable). */
807 pci_mod_config8(MCU, 0x6b, 0x07, 0x03); /* MSR Enable */
808
Martin Roth0cd338e2016-07-29 14:07:30 -0600809 /* Step 09 - Issue MR2 cycle. Read a double word from the address
810 * depended on DRAM's Rtt_WR and CWL settings. */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500811 mrs = ddr3_get_mr2(rtt_wr, srt, asr, cwl);
812 if (ma_swap)
813 mrs = ddr3_mrs_mirror_pins(mrs);
814 volatile_read(vx900_get_mrs_addr(mrs));
815 printram("MR2: %.5x\n", mrs);
816 udelay(1000);
817
Martin Roth0cd338e2016-07-29 14:07:30 -0600818 /* Step 10 - Issue MR3 cycle. Read a double word from the address 60000h
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500819 * to set DRAM to normal operation mode. */
820 mrs = ddr3_get_mr3(0);
821 if (ma_swap)
822 mrs = ddr3_mrs_mirror_pins(mrs);
823 volatile_read(vx900_get_mrs_addr(mrs));
824 printram("MR3: %.5x\n", mrs);
825 udelay(1000);
826
Martin Roth0cd338e2016-07-29 14:07:30 -0600827 /* Step 11 -Issue MR1 cycle. Read a double word from the address
828 * depended on DRAM's output driver impedance and Rtt_Nom settings.
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500829 * The DLL enable field, TDQS field, write leveling enable field,
830 * additive latency field and Qoff field should be set to 0. */
831 mrs = ddr3_get_mr1(DDR3_MR1_QOFF_ENABLE, DDR3_MR1_TQDS_DISABLE, rtt_nom,
832 DDR3_MR1_WRLVL_DISABLE, ods, DDR3_MR1_AL_DISABLE,
833 DDR3_MR1_DLL_ENABLE);
834 if (ma_swap)
835 mrs = ddr3_mrs_mirror_pins(mrs);
836 volatile_read(vx900_get_mrs_addr(mrs));
837 printram("MR1: %.5x\n", mrs);
838 udelay(1000);
839
840 /* Step 12 - Issue MR0 cycle. Read a double word from the address
Martin Roth0cd338e2016-07-29 14:07:30 -0600841 * depended on DRAM's burst length, CAS latency and write recovery time
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500842 * settings.
843 * The read burst type field should be set to interleave.
844 * The mode field should be set to normal mode.
845 * The DLL reset field should be set to No.
846 * The DLL control for precharge PD field should be set to Fast exit.
847 */
848 mrs = ddr3_get_mr0(DDR3_MR0_PRECHARGE_FAST, twr,
849 DDR3_MR0_DLL_RESET_NO, DDR3_MR0_MODE_NORMAL, cas,
850 DDR3_MR0_BURST_TYPE_INTERLEAVED,
851 DDR3_MR0_BURST_LENGTH_CHOP);
852 volatile_read(vx900_get_mrs_addr(mrs));
853 printram("MR0: %.5x\n", mrs);
854 udelay(1000);
855
856 /* Step 13 - Set Fun3_RX6B[2:0] to 110b (Long ZQ calibration cmd) */
857 pci_mod_config8(MCU, 0x6b, 0x07, 0x06); /* Long ZQ */
858 /* Step 14 - Read a double word from any address of the DIMM. */
859 volatile_read(0);
860 udelay(1000);
861}
862
863/*
864 * This is where we take the DIMMs out of reset and do the JEDEC dance for each
865 * individual physical rank.
866 */
867static void vx900_dram_ddr3_dimm_init(const ramctr_timing * ctrl,
868 const rank_layout * ranks)
869{
870 size_t i;
871 u8 rtt_nom, rtt_wr, ods, pinswap;
872
873 /* Set BA[0/1/2] to [A17/18/19] */
874 vx900_dram_set_ma_pin_map(VX900_MRS_MA_MAP);
875
876 /* Step 01 - Set Fun3_Rx6E[5] to 1b to support burst length. */
877 pci_mod_config8(MCU, 0x6e, 0, 1 << 5);
878 /* Step 02 - Set Fun3_RX69[0] to 0b (Disable Multiple Page Mode). */
879 pci_mod_config8(MCU, 0x69, (1 << 0), 0x00);
880 /* And set [7:6] to 10b ? */
881 pci_write_config8(MCU, 0x69, 0x87);
882
883 /* Step 03 - Set the target physical rank to virtual rank0 and other
884 * ranks to virtual rank3. */
885 vx900_pr_map_all_vr3();
886
887 /* Step 04 - Set Fun3_Rx50 to D8h. */
888 pci_write_config8(MCU, 0x50, 0xd8);
889 /* Step 05 - Set Fun3_RX6B[5] to 1b to de-assert RESET# and wait for at
890 * least 500 us. */
891 pci_mod_config8(MCU, 0x6b, 0x00, (1 << 5));
892 udelay(500);
893
894 /* Step 6 -> 15 - Set the target physical rank to virtual rank 0 and
895 * other ranks to virtual rank 3.
896 * Repeat Step 6 to 14 for every rank present, then jump to Step 16. */
897 for (i = 0; i < VX900_MAX_MEM_RANKS; i++) {
898 if (ranks->phys_rank_size_mb[i] == 0)
899 continue;
900 printram("Initializing rank %lu\n", i);
901
902 /* Set target physical rank to virtual rank 0
903 * other ranks to virtual rank 3*/
904 vx900_map_pr_vr(i, 0);
905
906 /* FIXME: Is this needed on HW init? */
907 pci_mod_config8(MCU, 0x6b, 0x07, 0x01); /* Enable NOP */
908 volatile_read(0x0); /* Do NOP */
909 pci_mod_config8(MCU, 0x6b, 0x07, 0x03); /* MSR Enable */
910
911 /* See init_dram_by_rank.c and get_basic_information.c
912 * in the VIA provided code */
913 if (ctrl->n_dimms == 1) {
914 rtt_nom = DDR3_MR1_RTT_NOM_RZQ2;
915 rtt_wr = DDR3_MR2_RTTWR_OFF;
916 } else {
917 rtt_nom = DDR3_MR1_RTT_NOM_RZQ8;
918 rtt_wr = DDR3_MR2_RTTWR_RZQ2;
919 }
920 ods = ranks->flags[i].rzq7_supported ?
921 DDR3_MR1_ODS_RZQ7 : DDR3_MR1_ODS_RZQ6;
922
923 pinswap = (ranks->flags[i].pins_mirrored);
924 if (pinswap)
925 printram("Pins mirrored\n");
926 printram(" Swap : %x\n", pinswap);
927 printram(" rtt_nom : %x\n", rtt_nom);
928 printram(" ods : %x\n", ods);
929 printram(" rtt_wr : %x\n", rtt_wr);
930 if (RAMINIT_USE_HW_MRS_SEQ)
931 vx900_dram_ddr3_do_hw_mrs(pinswap, rtt_nom, ods, rtt_wr,
932 0, 0);
933 else
934 vx900_dram_ddr3_do_sw_mrs(pinswap, rtt_nom, ods, rtt_wr,
935 0, 0);
936
937 /* Normal SDRAM Mode */
938 pci_mod_config8(MCU, 0x6b, 0x07, 0x00);
939
940 /* Step 15, set the rank to virtual rank 3 */
941 vx900_map_pr_vr(i, 3);
942 }
943
Martin Roth0cd338e2016-07-29 14:07:30 -0600944 /* Step 16 - Set Fun3_Rx6B[2:0] to 000b (Normal SDRAM Mode). */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500945 pci_mod_config8(MCU, 0x6b, 0x07, 0x00);
946
947 /* Set BA[0/1/2] to [A13/14/15] */
948 vx900_dram_set_ma_pin_map(VX900_CALIB_MA_MAP);
949
Martin Roth0cd338e2016-07-29 14:07:30 -0600950 /* Step 17 - Set Fun3_Rx69[0] to 1b (Enable Multiple Page Mode). */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -0500951 pci_mod_config8(MCU, 0x69, 0x00, (1 << 0));
952
953 printram("DIMM initialization sequence complete\n");
954}
955
956/*
957 * This a small utility to send a single MRS command, but where we don't want to
958 * have to worry about changing the MCU mode. It gives the MCU back to us in
959 * normal operating mode.
960 */
961static void vx900_dram_send_soft_mrs(mrs_cmd_t cmd, u8 pin_swap)
962{
963 u32 addr;
964 /* Set Fun3_RX6B[2:0] to 011b (MSR Enable). */
965 pci_mod_config8(MCU, 0x6b, 0x07, (3 << 0));
966 /* Is this a funky rank with Address pins swapped? */
967 if (pin_swap)
968 cmd = ddr3_mrs_mirror_pins(cmd);
969 /* Find the address corresponding to the MRS */
970 addr = vx900_get_mrs_addr(cmd);
971 /* Execute the MRS */
972 volatile_read(addr);
973 /* Set Fun3_Rx6B[2:0] to 000b (Normal SDRAM Mode). */
974 pci_mod_config8(MCU, 0x6b, 0x07, 0x00);
975}
976
977static void vx900_dram_enter_read_leveling(u8 pinswap)
978{
979 /* Precharge all before issuing read leveling MRS to DRAM */
980 pci_mod_config8(MCU, 0x06b, 0x07, 0x02);
981 volatile_read(0x0);
982 udelay(1000);
983
984 /* Enable read leveling: Set D0F3Rx71[7]=1 */
985 pci_mod_config8(MCU, 0x71, 0, (1 << 7));
986
987 /* Put DRAM in read leveling mode */
988 mrs_cmd_t cmd = ddr3_get_mr3(1);
989 vx900_dram_send_soft_mrs(cmd, pinswap);
990}
991
992static void vx900_dram_exit_read_leveling(u8 pinswap)
993{
994 /* Disable read leveling, and put dram in normal operation mode */
995 mrs_cmd_t cmd = ddr3_get_mr3(0);
996 vx900_dram_send_soft_mrs(cmd, pinswap);
997
998 /* Disable read leveling: Set D0F3Rx71[7]=0 */
999 pci_mod_config8(MCU, 0x71, (1 << 7), 0);
1000}
1001
1002/*
1003 * We need to see if the delay window (difference between minimum and maximum)
1004 * is large enough so that we actually have a valid window. The signal should be
1005 * valid for at least 1/2T in general. If the window is significantly smaller,
1006 * then chances are our window does not latch at the correct time, and the
1007 * calibration will not work.
1008 */
1009#define DQSI_THRESHOLD 0x10
1010#define DQO_THRESHOLD 0x09
1011#define DQSO_THRESHOLD 0x12
1012#define DELAY_RANGE_GOOD 0
1013#define DELAY_RANGE_BAD -1
1014static u8 vx900_dram_check_calib_range(const delay_range * dly, u8 window)
1015{
1016 size_t i;
1017 for (i = 0; i < 8; i++) {
1018 if (dly->high[i] - dly->low[i] < window)
1019 return DELAY_RANGE_BAD;
1020 /* When our maximum value is lower than our min, both values
1021 * have overshot, and the window is definitely invalid */
1022 if (dly->high[i] < dly->low[i])
1023 return DELAY_RANGE_BAD;
1024 }
1025 return DELAY_RANGE_GOOD;
1026}
1027
1028static void vx900_dram_find_avg_delays(vx900_delay_calib * delays)
1029{
1030 size_t i;
1031 u16 dq_low, dq_high, dqs_low, dqs_high, dq_final, dqs_final;
1032 /*
1033 * At this point, we have transmit delays for both DIMMA and DIMMB, each
1034 * with a slightly different window We want to find the intersection of
1035 * those windows, so that we have a constrained window which both
1036 * DIMMA and DIMMB can use. The center of our constrained window will
1037 * also be the safest setting for the transmit delays
1038 *
1039 * DIMMA window t:|xxxxxxxxxxxxxx---------------xxxxxxxxxxxxxxxxxxxxxxx|
1040 * DIMMB window t:|xxxxxxxxxxxxxxxxxxx---------------xxxxxxxxxxxxxxxxxx|
1041 * Safe window t:|xxxxxxxxxxxxxxxxxxx----------xxxxxxxxxxxxxxxxxxxxxxx|
1042 */
1043 delay_range *tx_dq_a = &(delays->tx_dq[0]);
1044 delay_range *tx_dq_b = &(delays->tx_dq[1]);
1045 delay_range *tx_dqs_a = &(delays->tx_dqs[0]);
1046 delay_range *tx_dqs_b = &(delays->tx_dqs[1]);
1047
1048 for (i = 0; i < 8; i++) {
Alexandru Gagniuc560433b2013-06-10 15:47:25 -05001049 dq_low = MAX(tx_dq_a->low[i], tx_dq_b->low[i]);
1050 dq_high = MIN(tx_dq_a->high[i], tx_dq_b->high[i]);
1051 dqs_low = MAX(tx_dqs_a->low[i], tx_dqs_b->low[i]);
1052 dqs_high = MIN(tx_dqs_a->high[i], tx_dqs_b->high[i]);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001053
1054 /* Find the average */
1055 dq_final = ((dq_low + dq_high) / 2);
1056 dqs_final = ((dqs_low + dqs_high) / 2);
1057
1058 /*
1059 * These adjustments are done in code provided by VIA.
1060 * There is no explanation as to why this is done.
1061 *
1062 * We can get away without doing the DQS adjustment, but doing
1063 * it, brings the values closer to what the vendor BIOS
1064 * calibrates to.
1065 */
1066 if ((dqs_final & 0x1f) >= 0x1c)
1067 dqs_final -= 0x1c;
1068 else
1069 dqs_final += 0x04;
1070 /*
1071 * The DQ adjustment is more critical. If we don't do this
1072 * adjustment our MCU won't be configured properly, and
1073 * ram_check() will fail.
1074 */
1075 if ((dq_final & 0x1f) >= 0x14)
1076 dq_final -= 0x14;
1077 else
1078 dq_final += 0x0c;
1079
1080 /* Store our values in the first delay */
1081 delays->tx_dq[0].avg[i] = dq_final;
1082 delays->tx_dqs[0].avg[i] = dqs_final;
1083
1084 }
1085}
1086
1087/*
1088 * First calibration: When to receive data from the DRAM
1089 * (MD and MDQS input delay)
1090 *
1091 * This calibration unfortunately does not seem to work. Whether this is due to
1092 * a misconfigured MCU or hardware bug is unknown.
1093 */
1094static void vx900_rx_capture_range_calib(u8 pinswap)
1095{
1096 u8 reg8;
1097 const u32 cal_addr = 0x20;
1098
1099 /* Set IO calibration address */
1100 pci_mod_config16(MCU, 0x8c, 0xfff0, cal_addr & (0xfff0));
1101 /* Data pattern must be 0x00 for this calibration
1102 * See paragraph describing Rx8e */
1103 pci_write_config8(MCU, 0x8e, 0x00);
1104
1105 /* Need to put DRAM and MCU in read leveling */
1106 vx900_dram_enter_read_leveling(pinswap);
1107
1108 /* Data pattern must be 0x00 for this calibration
1109 * See paragraph describing Rx8e */
1110 pci_write_config8(MCU, 0x8e, 0x00);
1111 /* Trigger calibration */
1112 reg8 = 0xa0;
1113 pci_write_config8(MCU, 0x71, reg8);
1114
1115 /* Wait for it */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001116 while (pci_read_config8(MCU, 0x71) & 0x10);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001117 vx900_dram_exit_read_leveling(pinswap);
1118}
1119
1120/*
1121 * Second calibration: How much to delay DQS signal by
1122 * (MDQS input delay)
1123 */
1124static void vx900_rx_dqs_delay_calib(u8 pinswap)
1125{
1126 const u32 cal_addr = 0x30;
1127
1128 /* We need to disable refresh commands so that they don't interfere */
1129 const u8 ref_cnt = pci_read_config8(MCU, 0xc7);
1130 pci_write_config8(MCU, 0xc7, 0);
1131 /* Set IO calibration address */
1132 pci_mod_config16(MCU, 0x8c, 0xfff0, cal_addr & (0xfff0));
1133 /* Data pattern must be 0x00 for this calibration
1134 * See paragraph describing Rx8e */
1135 pci_write_config8(MCU, 0x8e, 0x00);
1136
1137 /* Need to put DRAM and MCU in read leveling */
1138 vx900_dram_enter_read_leveling(pinswap);
1139
1140 /* From VIA code; Undocumented
1141 * In theory this enables MODT[3:0] to be asserted */
1142 pci_mod_config8(MCU, 0x9e, 0, 0x80);
1143
1144 /* Trigger calibration: Set D0F3Rx71[1:0]=10b */
1145 pci_mod_config8(MCU, 0x71, 0x03, 0x02);
1146
1147 /* Wait for calibration to complete */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001148 while (pci_read_config8(MCU, 0x71) & 0x02);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001149 vx900_dram_exit_read_leveling(pinswap);
1150
1151 /* Restore the refresh counter */
1152 pci_write_config8(MCU, 0xc7, ref_cnt);
1153
1154 /* FIXME: should we save it before, or should we just set it as is */
1155 vx900_dram_set_ma_pin_map(VX900_CALIB_MA_MAP);
1156}
1157
1158static void vx900_tx_dqs_trigger_calib(u8 pattern)
1159{
1160 /* Data pattern for calibration */
1161 pci_write_config8(MCU, 0x8e, pattern);
1162 /* Trigger calibration */
1163 pci_mod_config8(MCU, 0x75, 0, 0x20);
1164 /* Wait for calibration */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001165 while (pci_read_config8(MCU, 0x75) & 0x20);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001166}
1167
1168/*
1169 * Third calibration: How much to wait before asserting DQS
1170 */
1171static void vx900_tx_dqs_delay_calib(void)
1172{
1173 const u32 cal_addr = 0x00;
1174 /* Set IO calibration address */
1175 pci_mod_config16(MCU, 0x8c, 0xfff0, cal_addr & (0xfff0));
1176 /* Set circuit to use calibration results - Clear Rx75[0] */
1177 pci_mod_config8(MCU, 0x75, 0x01, 0);
1178 /* Run calibration with first data pattern */
1179 vx900_tx_dqs_trigger_calib(0x5a);
1180 /* Run again with different pattern */
1181 vx900_tx_dqs_trigger_calib(0xa5);
1182}
1183
1184/*
1185 * Fourt calibration: How much to wait before putting data on DQ lines
1186 */
1187static void vx900_tx_dq_delay_calib(void)
1188{
1189 /* Data pattern for calibration */
1190 pci_write_config8(MCU, 0x8e, 0x5a);
1191 /* Trigger calibration */
1192 pci_mod_config8(MCU, 0x75, 0, 0x02);
1193 /* Wait for calibration */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001194 while (pci_read_config8(MCU, 0x75) & 0x02);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001195}
1196
1197static void vx900_rxdqs_adjust(delay_range * dly)
1198{
1199 /* Adjust Rx DQS delay after calibration has been run. This is
1200 * recommended by VIA, but no explanation was provided as to why */
1201 size_t i;
1202 for (i = 0; i < 8; i++) {
1203 if (dly->low[i] < 3) {
1204 if (i == 2 || i == 4)
1205 dly->avg[i] += 4;
1206 else
1207 dly->avg[i] += 3;
1208
1209 }
1210
1211 if (dly->high[i] > 0x38)
1212 dly->avg[i] -= 6;
1213 else if (dly->high[i] > 0x30)
1214 dly->avg[i] -= 4;
1215
1216 if (dly->avg[i] > 0x20)
1217 dly->avg[i] = 0x20;
1218 }
1219
1220 /* Put Rx DQS delay into manual mode (Set Rx[2,0] to 01) */
1221 pci_mod_config8(MCU, 0x71, 0x05, 0x01);
1222 /* Now write the new settings */
1223 vx900_delay_calib_mode_select(CALIB_RxDQS, CALIB_MANUAL);
1224 vx900_write_0x78_0x7f(dly->avg);
1225}
1226
1227static void vx900_dram_calibrate_recieve_delays(vx900_delay_calib * delays,
1228 u8 pinswap)
1229{
1230 size_t n_tries = 0;
1231 delay_range *rx_dq_cr = &(delays->rx_dq_cr);
1232 delay_range *rx_dqs = &(delays->rx_dqs);
1233 /* We really should be able to finish this in a single pass, but it may
1234 * in very rare circumstances not work the first time. We define a limit
1235 * on the number of tries so that we have a way of warning the user */
1236 const size_t max_tries = 100;
1237 for (;;) {
1238 if (n_tries++ >= max_tries) {
1239 die("Could not calibrate receive delays. Giving up");
1240 }
1241 u8 result;
1242 /* Run calibrations */
1243 if (RAMINIT_USE_HW_RXCR_CALIB) {
1244 vx900_rx_capture_range_calib(pinswap);
1245 vx900_read_delay_range(rx_dq_cr, CALIB_RxDQ_CR);
1246 dump_delay_range(*rx_dq_cr);
1247
1248 } else {
1249 /*FIXME: Cheating with Rx CR setting\
1250 * We need to either use Rx CR calibration
1251 * or set up a table for the calibration */
1252 u8 *override = &(rx_dq_cr->avg[0]);
1253 override[0] = 0x28;
1254 override[1] = 0x1c;
1255 override[2] = 0x28;
1256 override[3] = 0x28;
1257 override[4] = 0x2c;
1258 override[5] = 0x30;
1259 override[6] = 0x30;
1260 override[7] = 0x34;
1261 printram("Bypassing RxCR 78-7f calibration with:\n");
1262 dump_delay(rx_dq_cr->avg);
1263 }
1264 /* We need to put the setting on manual mode */
1265 pci_mod_config8(MCU, 0x71, 0, 1 << 4);
1266 vx900_delay_calib_mode_select(CALIB_RxDQ_CR, CALIB_MANUAL);
1267 vx900_write_0x78_0x7f(rx_dq_cr->avg);
1268
1269 /************* RxDQS *************/
1270 vx900_rx_dqs_delay_calib(pinswap);
1271 vx900_read_delay_range(rx_dqs, CALIB_RxDQS);
1272 vx900_rxdqs_adjust(rx_dqs);
1273
1274 result = vx900_dram_check_calib_range(rx_dqs, DQSI_THRESHOLD);
1275 if (result != DELAY_RANGE_GOOD)
1276 continue;
1277
1278 /* We're good to go. Switch to manual and write the manual
1279 * setting */
1280 pci_mod_config8(MCU, 0x71, 0, 1 << 0);
1281 vx900_delay_calib_mode_select(CALIB_RxDQS, CALIB_MANUAL);
1282 vx900_write_0x78_0x7f(rx_dqs->avg);
1283 break;
1284 }
1285 if (n_tries > 1)
1286 printram("Hmm, we had to try %lu times before our calibration "
1287 "was good.\n", n_tries);
1288}
1289
1290static void vx900_dram_calibrate_transmit_delays(delay_range * tx_dq,
1291 delay_range * tx_dqs)
1292{
1293 /* Same timeout reasoning as in receive delays */
1294 size_t n_tries = 0;
Idwer Volleringd26da9c2013-12-22 21:38:18 +00001295 int dq_tries = 0, dqs_tries = 0;
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001296 const size_t max_tries = 100;
1297 for (;;) {
1298 if (n_tries++ >= max_tries) {
1299 printram("Tried DQS %i times and DQ %i times\n",
1300 dqs_tries, dq_tries);
1301 printram("Tx DQS calibration results\n");
1302 dump_delay_range(*tx_dqs);
1303 printram("TX DQ delay calibration results:\n");
1304 dump_delay_range(*tx_dq);
1305 die("Could not calibrate transmit delays. Giving up");
1306 }
1307 u8 result;
1308 /************* TxDQS *************/
1309 dqs_tries++;
1310 vx900_tx_dqs_delay_calib();
1311 vx900_read_delay_range(tx_dqs, CALIB_TxDQS);
1312
1313 result = vx900_dram_check_calib_range(tx_dqs, DQSO_THRESHOLD);
1314 if (result != DELAY_RANGE_GOOD)
1315 continue;
1316
1317 /************* TxDQ *************/
1318 /* FIXME: not sure if multiple page mode should be enabled here
1319 * Vendor BIOS does it */
1320 pci_mod_config8(MCU, 0x69, 0, 0x01);
1321
1322 dq_tries++;
1323 vx900_tx_dq_delay_calib();
1324 vx900_read_delay_range(tx_dq, CALIB_TxDQ);
1325
1326 result = vx900_dram_check_calib_range(tx_dq, DQO_THRESHOLD);
1327 if (result != DELAY_RANGE_GOOD)
1328 continue;
1329
1330 /* At this point, our RAM should give correct read-backs for
1331 * addresses under 64 MB. If it doesn't, it won't work */
1332 if (ram_check_noprint_nodie(1 << 20, 1 << 20)) {
1333 /* No, our RAM is not working, try again */
1334 /* FIXME: Except that we have not yet told the MCU what
1335 * the geometry of the DIMM is, hence we don't trust
1336 * this test for now */
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001337 }
1338 /* Good. We should be able to use this DIMM */
1339 /* That's it. We're done */
1340 break;
1341 }
1342 if (n_tries > 1)
1343 printram("Hmm, we had to try %lu times before our calibration "
1344 "was good.\n", n_tries);
1345}
1346
1347/*
1348 * The meat and potatoes of getting our MCU to operate the DIMMs properly.
1349 *
1350 * Thank you JEDEC for making us need configurable delays for each set of MD
1351 * signals.
1352 */
1353static void vx900_dram_calibrate_delays(const ramctr_timing * ctrl,
1354 const rank_layout * ranks)
1355{
1356 size_t i;
1357 u8 val;
1358 u8 dimm;
1359 vx900_delay_calib delay_cal;
1360 memset(&delay_cal, 0, sizeof(delay_cal));
1361 printram("Starting delay calibration\n");
1362
1363 /**** Read delay control ****/
1364 /* MD Input Data Push Timing Control;
1365 * use values recommended in datasheet
1366 * Setting this too low causes the Rx window to move below the range we
1367 * need it so we can capture it with Rx_78_7f
1368 * This causes Rx calibrations to be too close to 0, and Tx
1369 * calibrations will fail.
1370 * Setting this too high causes the window to move above the range.
1371 */
1372 if (ctrl->tCK <= TCK_533MHZ)
1373 val = 2;
1374 else if (ctrl->tCK <= TCK_333MHZ)
1375 val = 1;
1376 else
1377 val = 0;
1378 val++; /* FIXME: vendor BIOS sets this to 3 */
1379 pci_mod_config8(MCU, 0x74, (0x03 << 1), ((val & 0x03) << 1));
1380
1381 /* FIXME: The vendor BIOS increases the MD input delay - WHY ? */
1382 pci_mod_config8(MCU, 0xef, (3 << 4), 3 << 4);
1383
1384 /**** Write delay control ****/
1385 /* FIXME: The vendor BIOS does this, but WHY?
1386 * See check_special_registers in VIA provided code. This value seems
1387 * to depend on the DRAM frequency.
1388 */
1389 /* Early DQ/DQS for write cycles */
1390 pci_mod_config8(MCU, 0x76, (3 << 2), 2 << 2);
1391 /* FIXME: The vendor BIOS does this - Output preamble ? */
1392 pci_write_config8(MCU, 0x77, 0x10);
1393
1394 /* Set BA[0/1/2] to [A17/18/19] */
1395 vx900_dram_set_ma_pin_map(VX900_MRS_MA_MAP);
1396 /* Disable Multiple Page Mode - Set Rx69[0] to 0 */
1397 pci_mod_config8(MCU, 0x69, (1 << 0), 0x00);
1398
1399 /* It's very important that we keep all ranks which are not calibrated
1400 * mapped to VR3. Even if we disable them, if they are mapped to VR0
1401 * (the rank we use for calibrations), the calibrations may fail in
1402 * unexpected ways. */
1403 vx900_pr_map_all_vr3();
1404
1405 /* We only really need to run the receive calibrations once. They are
1406 * meant to account for signal travel differences in the internal paths
1407 * of the MCU, so it doesn't really matter which rank we use for this.
1408 * Differences between ranks will be accounted for in the transmit
1409 * calibration. */
1410 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i += 2) {
1411 /* Do we have a valid DIMM? */
1412 if (ranks->phys_rank_size_mb[i] +
1413 ranks->phys_rank_size_mb[i + 1] == 0)
1414 continue;
1415 /* Map the first rank of the DIMM to VR0 */
1416 vx900_map_pr_vr(2 * i, 0);
1417 /* Only run on first rank, remember? */
1418 break;
1419 }
1420 vx900_dram_calibrate_recieve_delays(&delay_cal,
1421 ranks->flags[i].pins_mirrored);
1422 printram("RX DQS calibration results\n");
1423 dump_delay_range(delay_cal.rx_dqs);
1424
1425 /* Enable multiple page mode for when calibrating transmit delays */
1426 pci_mod_config8(MCU, 0x69, 0, 1 << 1);
1427
1428 /*
1429 * Unlike the receive delays, we need to run the transmit calibration
1430 * for each DIMM (not rank). We run the calibration on the even rank.
1431 * The odd rank may have memory pins swapped, and this, it seems,
1432 * confuses the calibration circuit.
1433 */
1434 dimm = 0;
1435 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i++) {
1436 /* Do we have a valid DIMM? */
1437 u32 dimm_size_mb = ranks->phys_rank_size_mb[2 * i]
1438 + ranks->phys_rank_size_mb[2 * i + 1];
1439 if (dimm_size_mb == 0)
1440 continue;
1441 /* Map the first rank of the DIMM to VR0 */
1442 vx900_map_pr_vr(2 * i, 0);
1443 vx900_dram_calibrate_transmit_delays(&(delay_cal.tx_dq[dimm]),
1444 &(delay_cal.tx_dqs[dimm]));
1445 /* We run this more than once, so dump delays for each DIMM */
1446 printram("Tx DQS calibration results\n");
1447 dump_delay_range(delay_cal.tx_dqs[dimm]);
1448 printram("TX DQ delay calibration results:\n");
1449 dump_delay_range(delay_cal.tx_dq[dimm]);
1450 /* Now move the DIMM back to VR3 */
1451 vx900_map_pr_vr(2 * i, 3);
1452 /* We use dimm as a counter so that we fill tx_dq[] and tx_dqs[]
1453 * results in order from 0, and do not leave any gaps */
1454 dimm++;
1455 }
1456
1457 /* When we have more dimms, we need to find a tx window with which all
1458 * dimms can safely work */
1459 if (dimm > 1) {
1460 vx900_dram_find_avg_delays(&delay_cal);
1461 printram("Final delay values\n");
1462 printram("Tx DQS: ");
1463 dump_delay(delay_cal.tx_dqs[0].avg);
1464 printram("Tx DQ: ");
1465 dump_delay(delay_cal.tx_dq[0].avg);
1466 }
1467 /* Write manual settings */
1468 pci_mod_config8(MCU, 0x75, 0, 0x01);
1469 vx900_delay_calib_mode_select(CALIB_TxDQS, CALIB_MANUAL);
1470 vx900_write_0x78_0x7f(delay_cal.tx_dqs[0].avg);
1471 vx900_delay_calib_mode_select(CALIB_TxDQ, CALIB_MANUAL);
1472 vx900_write_0x78_0x7f(delay_cal.tx_dq[0].avg);
1473}
1474
1475static void vx900_dram_set_refresh_counter(ramctr_timing * ctrl)
1476{
1477 u8 reg8;
1478 /* Set DRAM refresh counter
1479 * Based on a refresh counter of 0x61 at 400MHz */
1480 reg8 = (TCK_400MHZ * 0x61) / ctrl->tCK;
1481 pci_write_config8(MCU, 0xc7, reg8);
1482}
1483
1484/*
1485 * Here, we map each rank somewhere in our address space. We don't really care
1486 * at this point if this will overlap the PCI config space. If needed, remapping
1487 * is done in ramstage, where we actually know how much PCI space we actually
1488 * need.
1489 */
1490static void vx900_dram_range(ramctr_timing * ctrl, rank_layout * ranks)
1491{
1492 size_t i, vrank = 0;
1493 u8 reg8;
1494 u32 ramsize_mb = 0, tolm_mb;
1495 const u32 TOLM_3_5G = (7 << 29);
1496 /* All unused physical ranks go to VR3. Otherwise, the MCU might be
1497 * trying to read or write from unused ranks, or even worse, write some
1498 * bits to the rank we want, and some to the unused ranks, even though
1499 * they are disabled. Since VR3 is the last virtual rank to be used, we
1500 * eliminate any ambiguities that the MCU may face. */
1501 vx900_pr_map_all_vr3();
1502 for (i = 0; i < VX900_MAX_MEM_RANKS; i++) {
1503 u32 rank_size_mb = ranks->phys_rank_size_mb[i];
1504 if (!rank_size_mb)
1505 continue;
1506
1507 /* vvvvvvvvvv FIXME: Fix odd rank init vvvvvvvvvv */
1508 if ((i & 1)) {
Patrick Georgi6f7e4b22014-05-19 09:18:11 +02001509 printk(BIOS_EMERG, "I cannot initialize rank %zu\n", i);
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001510 printk(BIOS_EMERG, "I have to disable it\n");
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001511 continue;
1512 }
1513 /* ^^^^^^^^^^ FIXME: Fix odd rank init ^^^^^^^^^^ */
1514
1515 ranks->virt[vrank].start_addr = ramsize_mb;
1516 ramsize_mb += rank_size_mb;
1517 ranks->virt[vrank].end_addr = ramsize_mb;
1518
1519 /* Rank memory range */
1520 reg8 = (ranks->virt[vrank].start_addr >> 6);
1521 pci_write_config8(MCU, 0x48 + vrank, reg8);
1522 reg8 = (ranks->virt[vrank].end_addr >> 6);
1523 pci_write_config8(MCU, 0x40 + vrank, reg8);
1524
1525 vx900_map_pr_vr(i, vrank);
1526
1527 printram("Mapped Physical rank %u, to virtual rank %u\n"
1528 " Start address: 0x%.10llx\n"
1529 " End address: 0x%.10llx\n",
1530 (int)i, (int)vrank,
1531 (u64) ranks->virt[vrank].start_addr << 20,
1532 (u64) ranks->virt[vrank].end_addr << 20);
1533 /* Move on to next virtual rank */
1534 vrank++;
1535 }
1536
1537 /* Limit the Top of Low memory at 3.5G
1538 * Not to worry, we'll set tolm in ramstage, once we have initialized
1539 * all devices and know pci_tolm. */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -05001540 tolm_mb = MIN(ramsize_mb, TOLM_3_5G >> 20);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001541 u16 reg_tolm = (tolm_mb << 4) & 0xfff0;
1542 pci_mod_config16(MCU, 0x84, 0xfff0, reg_tolm);
1543
1544 printram("Initialized %u virtual ranks, with a total size of %u MB\n",
1545 (int)vrank, ramsize_mb);
1546}
1547
1548/*
1549 * Here, we tell the memory controller how to treat a DIMM. This is an extremely
1550 * important step. It tells the MCU how many address bits we have in each DIMM,
1551 * and how to use them. This information is essential for the controller to
1552 * understand the DIMM addressing, and write and read data in the correct place.
1553 */
1554static void vx900_dram_map_row_col_bank(dimm_info * dimms)
1555{
1556 u8 reg8, rcb_val, col_bits, max_row_bits;
1557 size_t i;
1558 /* Do we have 4Gbit chips? */
1559 /* FIXME: Implement this */
1560
1561 /* Do we have 8Gbit chips? */
1562 /* FIXME: Implement this */
1563
1564 max_row_bits = rcb_val = reg8 = 0;
1565 for (i = 0; i < VX900_MAX_DIMM_SLOTS; i++) {
1566 if (dimms->dimm[i].dram_type == SPD_MEMORY_TYPE_UNDEFINED)
1567 continue;
1568
1569 col_bits = dimms->dimm[i].col_bits;
1570
1571 /*
1572 * DDR3 always uses 3 bank address bits, and MA type 111b cannot
1573 * be used due to chipset limitation. We are left with only two
1574 * options, which we can choose based solely on the number of
1575 * column address bits.
1576 */
1577 if ((col_bits < 10) || (col_bits > 11)) {
1578 printram("DIMM %ld has %d column address bits.\n",
1579 i, col_bits);
1580 die("Unsupported DIMM. Try booting without this DIMM");
1581 }
1582
1583 rcb_val = col_bits - 5;
1584 reg8 |= (rcb_val << ((i * 3) + 2));
1585
1586 /* */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -05001587 max_row_bits = MAX(max_row_bits, dimms->dimm[i].row_bits);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001588 }
1589
1590 printram("RCBA map (rx50) <- %.2x\n", reg8);
1591 pci_write_config8(MCU, 0x50, reg8);
1592
1593 printram("Houston, we have %d row address bits\n", max_row_bits);
1594 /* FIXME: Do this properly */
1595 vx900_dram_map_pins(13, 14, 15, 17, 16);
1596
1597}
1598
1599/*
1600 * Here, we set some final configuration bits, which should improve the
1601 * performance of the memory slightly (arbitration, expiration counters, etc.)
1602 *
1603 * FIXME: We don't really do much else than the minimum to get the MCU properly
1604 * configured. We don't yet do set the "performance-enhancing" bits referenced
1605 * in the comment above.
1606 */
1607static void vx900_dram_write_final_config(ramctr_timing * ctrl)
1608{
1609 /* FIXME: These are quick cheats */
1610
1611 /* FIXME: Why are we doing this? */
1612 /* Tri-state MCSi# when rank is in self-refresh */
1613 pci_mod_config8(MCU, 0x99, 0, 0x0f);
1614
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001615 /* Enable paging mode and 8 page registers */
1616 pci_mod_config8(MCU, 0x69, 0, 0xe5);
Alexandru Gagniuc7d31e7c2013-06-08 11:49:10 -05001617
1618 /* Enable automatic triggering of short ZQ calibration */
1619 pci_write_config8(MCU, 0xc8, 0x80);
1620
1621 /* And last but not least, Enable A20 line */
1622 outb(inb(0x92) | (1 << 1), 0x92);
1623}
1624
1625void vx900_init_dram_ddr3(const dimm_layout * dimm_addr)
1626{
1627 dimm_info dimm_prop;
1628 ramctr_timing ctrl_prop;
1629 rank_layout ranks;
1630 device_t mcu;
1631
1632 if (!ram_check_noprint_nodie(1 << 20, 1 << 20)) {
1633 printram("RAM is already initialized. Skipping init\n");
1634 return;
1635 }
1636 /* Locate the Memory controller */
1637 mcu = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
1638 PCI_DEVICE_ID_VIA_VX900_MEMCTRL), 0);
1639
1640 if (mcu == PCI_DEV_INVALID) {
1641 die("Memory Controller not found\n");
1642 }
1643 memset(&dimm_prop, 0, sizeof(dimm_prop));
1644 memset(&ctrl_prop, 0, sizeof(ctrl_prop));
1645 memset(&ranks, 0, sizeof(ranks));
1646 /* 1) Write some initial "safe" parameters */
1647 vx900_dram_write_init_config();
1648 /* 2) Get timing information from SPDs */
1649 dram_find_spds_ddr3(dimm_addr, &dimm_prop);
1650 /* 3) Find lowest common denominator for all modules */
1651 dram_find_common_params(&dimm_prop, &ctrl_prop);
1652 /* 4) Find the size of each memory rank */
1653 vx900_dram_phys_bank_range(&dimm_prop, &ranks);
1654 /* 5) Set DRAM driving strength */
1655 vx900_dram_driving_ctrl(&dimm_prop);
1656 /* 6) Set DRAM frequency and latencies */
1657 vx900_dram_timing(&ctrl_prop);
1658 vx900_dram_freq(&ctrl_prop);
1659 /* 7) Initialize the modules themselves */
1660 vx900_dram_ddr3_dimm_init(&ctrl_prop, &ranks);
1661 /* 8) Set refresh counter based on DRAM frequency */
1662 vx900_dram_set_refresh_counter(&ctrl_prop);
1663 /* 9) Calibrate receive and transmit delays */
1664 vx900_dram_calibrate_delays(&ctrl_prop, &ranks);
1665 /* 10) Enable Physical to Virtual Rank mapping */
1666 vx900_dram_range(&ctrl_prop, &ranks);
1667 /* 11) Map address bits to DRAM pins */
1668 vx900_dram_map_row_col_bank(&dimm_prop);
1669 /* 99) Some final adjustments */
1670 vx900_dram_write_final_config(&ctrl_prop);
1671 /* Take a dump */
1672 dump_pci_device(mcu);
1673}