Debian Codegraph @

src/northbridge/via/cx700/raminit.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <types.h>
4 #include <delay.h>
5 #include <spd.h>
6 #include <device/mmio.h>
7 #include <device/pci_ops.h>
8 #include <device/smbus_host.h>
9 #include <device/dram/common.h>
10 #include <static_devices.h>
11
12 #include "chip.h"
13 #include "registers.h"
14 #include "raminit.h"
15
16 struct mem_controller {
17 struct dram_cfg cfg;
18 };
19
20 #define SPD_MOD_ATTRIB_RANK 5
21 #define SPD_MOD_ATTRIB_RANK_NUM_MASK 0x07
22 #define SPD_PRI_WIDTH 13
23 #define SPD_CAS_LAT 18
24 #define SPD_CAS_LATx(x) (1 << (x))
25 #define SPD_CAS_LAT_MIN_X_1 23
26 #define SPD_CAS_LAT_MIN_X_2 25
27 #define SPD_TRP 27
28 #define SPD_TRRD 28
29 #define SPD_TRCD 29
30 #define SPD_TRAS 30
31 #define SPD_RANK_SIZE 31 /* Only one bit is set */
32 #define SPD_TWR 36
33 #define SPD_TWTR 37
34 #define SPD_TRTP 38
35 #define SPD_EX_TRC_TRFC 40
36 #define SPD_TRFC 42
37
38 #define RAM_COMMAND_NORMAL 0x0
39 #define RAM_COMMAND_NOP 0x1
40 #define RAM_COMMAND_PRECHARGE 0x2
41 #define RAM_COMMAND_MRS 0x3
42 #define RAM_COMMAND_CBR 0x4
43
44 #define HOSTCTRL _sdev_host_if
45 #define MEMCTRL _sdev_dram_ctrl
46 #define SCRATCH _sdev_pm_ctrl
47
48 #define SDRAM1X_RA_14 31
49 #define SDRAM1X_RA_13 30
50 #define SDRAM1X_RA_12 29
51 #define SDRAM1X_RA_12_8bk 27
52 #define SDRAM1X_CA_12 16
53 #define SDRAM1X_CA_11 15
54 #define SDRAM1X_CA_09 12
55 #define SDRAM1X_CA_09_8bk 12
56 #define SDRAM1X_BA1 14
57 #define SDRAM1X_BA2_8bk 15
58 #define SDRAM1X_BA1_8bk 14
59
60 #define MA_Column 0x06
61 #define MA_Bank 0x08
62 #define MA_Row 0x30
63 #define MA_4_Bank 0x00
64 #define MA_8_Bank 0x08
65 #define MA_12_Row 0x00
66 #define MA_13_Row 0x10
67 #define MA_14_Row 0x20
68 #define MA_15_Row 0x30
69 #define MA_9_Column 0x00
70 #define MA_10_Column 0x02
71 #define MA_11_Column 0x04
72 #define MA_12_Column 0x06
73
74 static u8 get_spd_data(const struct dram_cfg *cfg, unsigned int socket, u8 reg)
75 {
76 return cfg->spd_addr[socket] ? smbus_read_byte(cfg->spd_addr[socket], reg) : 0x00;
77 }
78
79 #define GET_SPD(i, val, tmp, reg) \
80 do { \
81 val = 0; \
82 tmp = 0; \
83 for (i = 0; i < DIMM_SOCKETS; i++) { \
84 if (pci_read_config8(SCRATCH, SCRATCH_RANK_0 + (i << 1))) { \
85 tmp = get_spd_data(&ctrl->cfg, i, reg); \
86 if (tmp > val) \
87 val = tmp; \
88 } \
89 } \
90 } while (0)
91
92 #define REGISTERPRESET(bus, dev, fun, bdfspec) \
93 { u8 j, reg; \
94 for (j = 0; j < ARRAY_SIZE(bdfspec); j++) { \
95 printk(BIOS_SPEW, "Writing bus " #bus " dev " #dev " fun " #fun " register "); \
96 printk(BIOS_SPEW, "%02x", (bdfspec)[j].reg); \
97 printk(BIOS_SPEW, "\n"); \
98 reg = pci_read_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[j].reg); \
99 reg &= (bdfspec)[j].mask; \
100 reg |= (bdfspec)[j].val; \
101 pci_write_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[j].reg, reg); \
102 } \
103 }
104
105 static const u8 Reg_Val[] = {
106 /* REG, VALUE */
107 0x70, 0x33,
108 0x71, 0x11,
109 0x72, 0x33,
110 0x73, 0x11,
111 0x74, 0x20,
112 0x75, 0x2e,
113 0x76, 0x64,
114 0x77, 0x00,
115 0x78, 0x44,
116 0x79, 0xaa,
117 0x7a, 0x33,
118 0x7b, 0xaa,
119 0x7c, 0x00,
120 0x7e, 0x33,
121 0x7f, 0x33,
122 0x80, 0x44,
123 0x81, 0x44,
124 0x82, 0x44,
125 0x83, 0x02,
126 0x50, 0x88,
127 0x51, 0x7b,
128 0x52, 0x6f,
129 0x53, 0x88,
130 0x54, 0x0e,
131 0x55, 0x00,
132 0x56, 0x00,
133 0x59, 0x00,
134 0x5d, 0x72,
135 0x5e, 0x88,
136 0x5f, 0xc7,
137 0x68, 0x01,
138 };
139
140 /* Host registers initial value */
141 static const u8 Host_Reg_Val[] = {
142 /* REG, VALUE */
143 0x60, 0xff,
144 0x61, 0xff,
145 0x62, 0x0f,
146 0x63, 0xff,
147 0x64, 0xff,
148 0x65, 0x0f,
149 0x66, 0xff,
150 0x67, 0x30,
151 };
152
153 static const u8 Mem_Reg_Init[] = {
154 /* REG, AND, OR */
155 0x50, 0x11, 0x66,
156 0x51, 0x11, 0x66,
157 0x52, 0x00, 0x11,
158 0x53, 0x00, 0x0f,
159 0x54, 0x00, 0x00,
160 0x55, 0x00, 0x00,
161 0x56, 0x00, 0x00,
162 0x57, 0x00, 0x00,
163 0x60, 0x00, 0x00,
164 0x62, 0xf7, 0x08,
165 0x65, 0x00, 0xd9,
166 0x66, 0x00, 0x80,
167 0x67, 0x00, 0x50, /* OR 0x00 ?? */
168 0x69, 0xf0, 0x00,
169 0x6a, 0x00, 0x00,
170 0x6d, 0xcf, 0xc0,
171 0x6e, 0xff, 0x80,
172 0x75, 0x0f, 0x40,
173 0x77, 0x00, 0x00,
174 0x80, 0x00, 0x00,
175 0x81, 0x00, 0x00,
176 0x82, 0x00, 0x00,
177 0x83, 0x00, 0x00,
178 0x84, 0x00, 0x00,
179 0x85, 0x00, 0x00,
180 0x86, 0xff, 0x2c, /* OR 0x28 if we don't want enable top 1M SM memory */
181 0x40, 0x00, 0x00,
182 0x7c, 0x00, 0x00,
183 0x7e, 0x00, 0x00,
184 0xa4, 0xfe, 0x00,
185 0xb0, 0x7f, 0x80,
186 0xb1, 0x00, 0xaa,
187 0xb4, 0xfd, 0x02,
188 0xb8, 0xfe, 0x00,
189 };
190
191 static const u8 Dram_Driving_ODT_CTRL[] = {
192 /* REG, VALUE */
193 0xd6, 0xa8,
194 0xd4, 0x80,
195 0xd0, 0x88,
196 0xd3, 0x01,
197 0xd8, 0x00,
198 0xda, 0x80,
199 };
200
201 #define Rank0_ODT 0x00
202 #define Rank1_ODT 0x01
203 #define Rank2_ODT 0x02
204 #define Rank3_ODT 0x03
205 #define NA_ODT 0x00
206 #define NB_ODT_75ohm 0x00
207 #define NB_ODT_150ohm 0x01
208 #define DDR2_ODT_75ohm 0x20
209 #define DDR2_ODT_150ohm 0x40
210
211 static const u8 ODT_TBL[] = {
212 /* RankMap, ODT Control Bits, DRAM & NB ODT setting */
213 0x01, ((NA_ODT << 6) | (NA_ODT << 4) | (NA_ODT << 2) | Rank0_ODT), (DDR2_ODT_150ohm | NB_ODT_75ohm),
214 0x03, ((NA_ODT << 6) | (NA_ODT << 4) | (Rank0_ODT << 2) | Rank1_ODT), (DDR2_ODT_150ohm | NB_ODT_75ohm),
215 0x04, ((NA_ODT << 6) | (Rank2_ODT << 4) | (NA_ODT << 2) | NA_ODT), (DDR2_ODT_150ohm | NB_ODT_75ohm),
216 0x05, ((NA_ODT << 6) | (Rank0_ODT << 4) | (NA_ODT << 2) | Rank2_ODT), (DDR2_ODT_75ohm | NB_ODT_150ohm),
217 0x07, ((NA_ODT << 6) | (Rank0_ODT << 4) | (Rank2_ODT << 2) | Rank2_ODT), (DDR2_ODT_75ohm | NB_ODT_150ohm),
218 0x0c, ((Rank2_ODT << 6) | (Rank3_ODT << 4) | (NA_ODT << 2) | NA_ODT), (DDR2_ODT_150ohm | NB_ODT_75ohm),
219 0x0d, ((Rank0_ODT << 6) | (Rank0_ODT << 4) | (NA_ODT << 2) | Rank2_ODT), (DDR2_ODT_75ohm | NB_ODT_150ohm),
220 0x0f, ((Rank0_ODT << 6) | (Rank0_ODT << 4) | (Rank2_ODT << 2) | Rank2_ODT), (DDR2_ODT_75ohm | NB_ODT_150ohm),
221 };
222
223 static const u8 DQS_DQ_TBL[] = {
224 /* RxE0: DRAM Timing DQS */
225 /* RxE2: DRAM Timing DQ */
226 /* RxE0, RxE2 */
227 0xee, 0xba,
228 0xee, 0xba,
229 0xcc, 0xba,
230 0xcc, 0xba,
231 };
232
233 static const u8 Duty_Control_DDR2[] = {
234 /* RxEC, RxED, RxEE, RXEF */
235 /* DDRII533 1~2 rank, DDRII400 */
236 0x84, 0x10, 0x00, 0x10,
237 /* DDRII533 3~4 rank */
238 0x44, 0x10, 0x00, 0x10,
239 };
240
241 static const u8 ChA_Clk_Phase_DDR2_Table[] = {
242 /* Rx91, Rx92, Rx93 */
243 /* DDRII533 1 rank */
244 0x04, 0x05, 0x06,
245 /* DDRII533 2~4 rank */
246 0x04, 0x05, 0x05,
247 /* DDRII400 */
248 0x02, 0x04, 0x04,
249 };
250
251 static const u8 DQ_DQS_Table[] = {
252 /* REG, VALUE */
253 /* DRAM DQ/DQS Output Delay Control */
254 0xdc, 0x65,
255 0xdd, 0x01,
256 0xde, 0xc0,
257 /* DRAM DQ/DQS input Capture Control */
258 0x78, 0x83,
259 0x79, 0x83,
260 0x7a, 0x00,
261 };
262
263 static const u8 DQSOChA_DDR2_Driving_Table[] = {
264 /* Rx70, Rx71 */
265 /* DDRII533 1~2 rank */
266 0x00, 0x01,
267 /* DDRII533 3~4 rank */
268 0x03, 0x00,
269 /* DDRII400 1~2 rank */
270 0x00, 0x04,
271 /* DDRII400 3~4 rank */
272 0x00, 0x01,
273 };
274
275 /************************************************************************/
276 /* Chipset Performance UP and other setting after DRAM Sizing Registers */
277 /************************************************************************/
278 static const u8 Dram_Table[] = {
279 /* REG, AND, OR */
280 0x60, 0xff, 0x03,
281 0x66, 0xcf, 0x80,
282 0x68, 0x00, 0x00,
283 0x69, 0xfd, 0x03,
284 0x6e, 0xff, 0x01,
285 0x95, 0xff, 0x40,
286 };
287
288 static const u8 Host_Table[] = {
289 /* REG, AND, OR */
290 0x51, 0x81, 0x7a,
291 0x55, 0xff, 0x06,
292 0x5e, 0x00, 0x88,
293 0x5d, 0xff, 0xb2,
294 };
295
296 static const u8 Init_Rank_Reg_Table[] = {
297 /* Rank Ending Address Registers */
298 0x40, 0x41, 0x42, 0x43,
299 /* Rank Beginning Address Registers */
300 0x48, 0x49, 0x4a, 0x4b,
301 /* Physical-to-Virtual Rank Mapping Registers */
302 0x54, 0x55,
303 };
304
305 static const u16 DDR2_MRS_table[] = {
306 /* CL: 2, 3, 4, 5 */
307 0x150, 0x1d0, 0x250, 0x2d0, /* BL = 4; Use 1X-bandwidth MA table to init DRAM */
308 0x158, 0x1d8, 0x258, 0x2d8, /* BL = 8; Use 1X-bandwidth MA table to init DRAM */
309 };
310
311 #define MRS_DDR2_TWR2 ((0 << 15) | (0 << 20) | (1 << 12))
312 #define MRS_DDR2_TWR3 ((0 << 15) | (1 << 20) | (0 << 12))
313 #define MRS_DDR2_TWR4 ((0 << 15) | (1 << 20) | (1 << 12))
314 #define MRS_DDR2_TWR5 ((1 << 15) | (0 << 20) | (0 << 12))
315 static const u32 DDR2_Twr_table[] = {
316 MRS_DDR2_TWR2,
317 MRS_DDR2_TWR3,
318 MRS_DDR2_TWR4,
319 MRS_DDR2_TWR5,
320 };
321
322 static const u8 DQSI_Rate_Table[] = {
323 8, /* DDRII 200 */
324 8, /* DDRII 266 */
325 8, /* DDRII 333 */
326 7, /* DDRII 400 */
327 8, /* DDRII 533 */
328 8, /* DDRII 666 */
329 };
330
331 static const u8 REFC_Table[] = {
332 0x65, 0x32, /* DDRII 100 */
333 0x86, 0x43, /* DDRII 266 */
334 0xa8, 0x54, /* DDRII 333 */
335 0xca, 0x65, /* DDRII 400 */
336 0xca, 0x86, /* DDRII 533 */
337 0xca, 0xa8, /* DDRII 666 */
338 };
339
340 static void do_ram_command(const struct mem_controller *ctrl, u8 command)
341 {
342 u8 reg;
343
344 reg = pci_read_config8(MEMCTRL, 0x6b);
345 reg &= 0xf8; /* Clear bits 2-0. */
346 reg |= command;
347 pci_write_config8(MEMCTRL, 0x6b, reg);
348
349 printram(" Sending RAM command 0x%02x\n", reg);
350 }
351
352 // TODO factor out to another file
353 static void c7_cpu_setup(const struct mem_controller *ctrl)
354 {
355 for (size_t i = 0; i < ARRAY_SIZE(Reg_Val); i += 2)
356 pci_write_config8(HOSTCTRL, Reg_Val[i], Reg_Val[i + 1]);
357 }
358
359 static void ddr_detect(const struct mem_controller *ctrl)
360 {
361 /* FIXME: Only supports 2 ranks per DIMM */
362 u8 val, rsize, dimm;
363 u8 nrank = 0;
364 u8 ndimm = 0;
365 u8 rmap = 0;
366 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
367 val = get_spd_data(&ctrl->cfg, dimm, 0);
368 if ((val != 0x80) && (val != 0xff))
369 continue;
370
371 printk(BIOS_DEBUG, "Found DIMM #%u\n", dimm);
372 ndimm++;
373
374 rsize = get_spd_data(&ctrl->cfg, dimm, SPD_RANK_SIZE);
375 /* unit is 128M */
376 rsize = (rsize << 3) | (rsize >> 5);
377
378 val = get_spd_data(&ctrl->cfg, dimm, SPD_MOD_ATTRIB_RANK);
379 switch (val & SPD_MOD_ATTRIB_RANK_NUM_MASK) {
380 case 1:
381 pci_write_config8(SCRATCH, SCRATCH_RANK_1 + (dimm << 1), rsize);
382 rmap |= 1 << ((dimm << 1) + 1);
383 nrank++;
384 __fallthrough;
385 case 0:
386 pci_write_config8(SCRATCH, SCRATCH_RANK_0 + (dimm << 1), rsize);
387 rmap |= 1 << (dimm << 1);
388 nrank++;
389 }
390 }
391 pci_write_config8(SCRATCH, SCRATCH_DIMM_NUM, ndimm);
392 pci_write_config8(SCRATCH, SCRATCH_RANK_NUM, nrank);
393 pci_write_config8(SCRATCH, SCRATCH_RANK_MAP, rmap);
394 }
395
396 static void sdram_set_safe_values(const struct mem_controller *ctrl)
397 {
398 /* The purpose of this function is to set initial values for the dram
399 * size and timings. It will be replaced with the SPD based function
400 * once the RAM commands are working with these values.
401 */
402 u8 regs, val, t, dimm;
403 u32 spds, tmp;
404
405 regs = pci_read_config8(MEMCTRL, 0x6c);
406 if (regs & (1 << 6))
407 printk(BIOS_DEBUG, "DDR2 Detected.\n");
408 else
409 die("ERROR: DDR1 memory detected but not supported by coreboot.\n");
410
411 /* Enable DDR2 */
412 regs |= (1 << 7);
413 pci_write_config8(MEMCTRL, 0x6c, regs);
414
415 /* SPD 5 # of ranks */
416 pci_write_config8(MEMCTRL, 0x6d, 0xc0);
417
418 /**********************************************/
419 /* Set DRAM Freq (DDR2 533) */
420 /**********************************************/
421 /* SPD 9 SDRAM Cycle Time */
422 GET_SPD(dimm, spds, regs, 9);
423
424 printk(BIOS_DEBUG, "DDRII ");
425 if (spds <= 0x3d) {
426 printk(BIOS_DEBUG, "533\n");
427 val = DDRII_533;
428 t = 38;
429 } else if (spds <= 0x50) {
430 printk(BIOS_DEBUG, "400\n");
431 val = DDRII_400;
432 t = 50;
433 } else if (spds <= 0x60) {
434 printk(BIOS_DEBUG, "333\n");
435 val = DDRII_333;
436 t = 60;
437 } else if (spds <= 0x75) {
438 printk(BIOS_DEBUG, "266\n");
439 val = DDRII_266;
440 t = 75;
441 } else {
442 printk(BIOS_DEBUG, "200\n");
443 val = DDRII_200;
444 t = 100;
445 }
446 /* To store DDRII frequency */
447 pci_write_config8(SCRATCH, SCRATCH_DRAM_FREQ, val);
448
449 /* Manual reset and adjust DLL when DRAM change frequency
450 * This is a necessary sequence.
451 */
452 udelay(2000);
453 regs = pci_read_config8(MEMCTRL, 0x90);
454 regs |= 0x7;
455 pci_write_config8(MEMCTRL, 0x90, regs);
456 udelay(2000);
457 regs = pci_read_config8(MEMCTRL, 0x90);
458 regs &= ~0x7;
459 regs |= val;
460 pci_write_config8(MEMCTRL, 0x90, regs);
461 udelay(2000);
462 regs = pci_read_config8(MEMCTRL, 0x6b);
463 regs |= 0xc0;
464 regs &= ~0x10;
465 pci_write_config8(MEMCTRL, 0x6b, regs);
466 udelay(1);
467 regs |= 0x10;
468 pci_write_config8(MEMCTRL, 0x6b, regs);
469 udelay(1);
470 regs &= ~0xc0;
471 pci_write_config8(MEMCTRL, 0x6b, regs);
472 regs = pci_read_config8(MEMCTRL, 0x6f);
473 regs |= 0x1;
474 pci_write_config8(MEMCTRL, 0x6f, regs);
475
476 /**********************************************/
477 /* Set DRAM Timing Setting (DDR2 533) */
478 /**********************************************/
479 /* SPD 9 18 23 25 CAS Latency NB3DRAM_REG62[2:0] */
480 /* Read SPD byte 18 CAS Latency */
481 GET_SPD(dimm, spds, regs, SPD_CAS_LAT);
482 printk(BIOS_DEBUG, "CAS Supported ");
483 if (spds & SPD_CAS_LATx(2))
484 printk(BIOS_DEBUG, "2 ");
485 if (spds & SPD_CAS_LATx(3))
486 printk(BIOS_DEBUG, "3 ");
487 if (spds & SPD_CAS_LATx(4))
488 printk(BIOS_DEBUG, "4 ");
489 if (spds & SPD_CAS_LATx(5))
490 printk(BIOS_DEBUG, "5 ");
491 if (spds & SPD_CAS_LATx(6))
492 printk(BIOS_DEBUG, "6");
493
494 /* We don't consider CAS = 6, because CX700 doesn't support it */
495 printk(BIOS_DEBUG, "\n CAS:");
496 if (spds & SPD_CAS_LATx(5)) {
497 printk(BIOS_DEBUG, "Starting at CL5");
498 val = 0x3;
499 /* See whether we can improve it */
500 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
501 if ((spds & SPD_CAS_LATx(4)) && (tmp < 0x50)) {
502 printk(BIOS_DEBUG, "\n... going to CL4");
503 val = 0x2;
504 }
505 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
506 if ((spds & SPD_CAS_LATx(3)) && (tmp < 0x50)) {
507 printk(BIOS_DEBUG, "\n... going to CL3");
508 val = 0x1;
509 }
510 } else {
511 printk(BIOS_DEBUG, "Starting at CL4");
512 val = 0x2;
513 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
514 if ((spds & SPD_CAS_LATx(3)) && (tmp < 0x50)) {
515 printk(BIOS_DEBUG, "\n... going to CL3");
516 val = 0x1;
517 }
518 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
519 if ((spds & SPD_CAS_LATx(2)) && (tmp < 0x50)) {
520 printk(BIOS_DEBUG, "\n... going to CL2");
521 val = 0x0;
522 }
523 }
524 regs = pci_read_config8(MEMCTRL, 0x62);
525 regs &= ~0x7;
526 regs |= val;
527 pci_write_config8(MEMCTRL, 0x62, regs);
528 printk(BIOS_DEBUG, "\n");
529
530 /* SPD 27 Trp NB3DRAM_REG64[3:2] */
531 GET_SPD(dimm, spds, regs, SPD_TRP);
532 printk(BIOS_DEBUG, "Trp %d\n", spds);
533 spds >>= 2;
534 for (val = 2; val <= 5; val++) {
535 if (spds <= (val * t / 10)) {
536 val = val - 2;
537 break;
538 }
539 }
540 val <<= 2;
541 regs = pci_read_config8(MEMCTRL, 0x64);
542 regs &= ~0xc;
543 regs |= val;
544 pci_write_config8(MEMCTRL, 0x64, regs);
545
546 /* SPD 29 Trcd NB3DRAM_REG64[7:6] */
547 GET_SPD(dimm, spds, regs, SPD_TRCD);
548 printk(BIOS_DEBUG, "Trcd %d\n", spds);
549 spds >>= 2;
550 for (val = 2; val <= 5; val++) {
551 if (spds <= (val * t / 10)) {
552 val = val - 2;
553 break;
554 }
555 }
556 val <<= 6;
557 regs = pci_read_config8(MEMCTRL, 0x64);
558 regs &= ~0xc0;
559 regs |= val;
560 pci_write_config8(MEMCTRL, 0x64, regs);
561
562 /* SPD 30 Tras NB3DRAM_REG62[7:4] */
563 GET_SPD(dimm, spds, regs, SPD_TRAS);
564 printk(BIOS_DEBUG, "Tras %d\n", spds);
565 for (val = 5; val <= 20; val++) {
566 if (spds <= (val * t / 10)) {
567 val = val - 5;
568 break;
569 }
570 }
571 val <<= 4;
572 regs = pci_read_config8(MEMCTRL, 0x62);
573 regs &= ~0xf0;
574 regs |= val;
575 pci_write_config8(MEMCTRL, 0x62, regs);
576
577 /* SPD 42 SPD 40 Trfc NB3DRAM_REG61[5:0] */
578 GET_SPD(dimm, spds, regs, SPD_TRFC);
579 printk(BIOS_DEBUG, "Trfc %d\n", spds);
580 tmp = spds;
581 GET_SPD(dimm, spds, regs, SPD_EX_TRC_TRFC);
582 if (spds & 0x1)
583 tmp += 256;
584 if (spds & 0xe)
585 tmp++;
586 for (val = 8; val <= 71; val++) {
587 if (tmp <= (val * t / 10)) {
588 val = val - 8;
589 break;
590 }
591 }
592 regs = pci_read_config8(MEMCTRL, 0x61);
593 regs &= ~0x3f;
594 regs |= val;
595 pci_write_config8(MEMCTRL, 0x61, regs);
596
597 /* SPD 28 Trrd NB3DRAM_REG63[7:6] */
598 GET_SPD(dimm, spds, regs, SPD_TRRD);
599 spds >>= 2;
600 for (val = 2; val <= 5; val++) {
601 if (spds <= (val * t / 10)) {
602 val = val - 2;
603 break;
604 }
605 }
606 val <<= 6;
607 printk(BIOS_DEBUG, "Trrd val = 0x%x\n", val);
608
609 regs = pci_read_config8(MEMCTRL, 0x63);
610 regs &= ~0xc0;
611 regs |= val;
612 pci_write_config8(MEMCTRL, 0x63, regs);
613
614 /* SPD 36 Twr NB3DRAM_REG61[7:6] */
615 GET_SPD(dimm, spds, regs, SPD_TWR);
616 for (val = 2; val <= 5; val++) {
617 if (spds <= (val * t / 10)) {
618 val = val - 2;
619 break;
620 }
621 }
622 val <<= 6;
623 printk(BIOS_DEBUG, "Twr val = 0x%x\n", val);
624
625 regs = pci_read_config8(MEMCTRL, 0x61);
626 regs &= ~0xc0;
627 regs |= val;
628 pci_write_config8(MEMCTRL, 0x61, regs);
629
630 /* SPD 37 Twtr NB3DRAM_REG63[1] */
631 GET_SPD(dimm, spds, regs, SPD_TWTR);
632 spds >>= 2;
633 printk(BIOS_DEBUG, "Twtr 0x%x\n", spds);
634 if (spds <= (t * 2 / 10))
635 val = 0;
636 else
637 val = 1;
638 val <<= 1;
639 printk(BIOS_DEBUG, "Twtr val = 0x%x\n", val);
640
641 regs = pci_read_config8(MEMCTRL, 0x63);
642 regs &= ~0x2;
643 regs |= val;
644 pci_write_config8(MEMCTRL, 0x63, regs);
645
646 /* SPD 38 Trtp NB3DRAM_REG63[3] */
647 GET_SPD(dimm, spds, regs, SPD_TRTP);
648 spds >>= 2;
649 printk(BIOS_DEBUG, "Trtp 0x%x\n", spds);
650 if (spds <= (t * 2 / 10))
651 val = 0;
652 else
653 val = 1;
654 val <<= 3;
655 printk(BIOS_DEBUG, "Trtp val = 0x%x\n", val);
656
657 regs = pci_read_config8(MEMCTRL, 0x63);
658 regs &= ~0x8;
659 regs |= val;
660 pci_write_config8(MEMCTRL, 0x63, regs);
661
662 /**********************************************/
663 /* Set DRAM DRDY Setting */
664 /**********************************************/
665 /* Write slowest value to register */
666 for (val = 0; val < ARRAY_SIZE(Host_Reg_Val); val += 2)
667 pci_write_config8(HOSTCTRL, Host_Reg_Val[val], Host_Reg_Val[val + 1]);
668
669 /* F2_RX51[7]=0, disable DRDY timing */
670 regs = pci_read_config8(HOSTCTRL, 0x51);
671 regs &= ~0x80;
672 pci_write_config8(HOSTCTRL, 0x51, regs);
673
674 /**********************************************/
675 /* Set DRAM BurstLength */
676 /**********************************************/
677 regs = pci_read_config8(MEMCTRL, 0x6c);
678 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
679 if (pci_read_config8(SCRATCH, SCRATCH_REG_BASE + (dimm << 1))) {
680 spds = get_spd_data(&ctrl->cfg, dimm, 16);
681 if (!(spds & 0x8))
682 break;
683 }
684 }
685 if (dimm == 2)
686 regs |= 0x8;
687 pci_write_config8(MEMCTRL, 0x6c, regs);
688 val = pci_read_config8(HOSTCTRL, 0x54);
689 val &= ~0x10;
690 if (dimm == 2)
691 val |= 0x10;
692 pci_write_config8(HOSTCTRL, 0x54, val);
693
694 /**********************************************/
695 /* Set DRAM Driving Setting */
696 /**********************************************/
697 /* DRAM Timing ODT */
698 for (val = 0; val < ARRAY_SIZE(Dram_Driving_ODT_CTRL); val += 2)
699 pci_write_config8(MEMCTRL, Dram_Driving_ODT_CTRL[val],
700 Dram_Driving_ODT_CTRL[val + 1]);
701
702 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_NUM);
703 val = pci_read_config8(MEMCTRL, 0xd5);
704 val &= ~0xaa;
705 switch (regs) {
706 case 3:
707 case 2:
708 val |= 0xa0;
709 break;
710 default:
711 val |= 0x80;
712 }
713 regs = pci_read_config8(SCRATCH, SCRATCH_DIMM_NUM);
714 if (regs == 1)
715 val |= 0xa;
716 pci_write_config8(MEMCTRL, 0xd5, val);
717
718 regs = pci_read_config8(SCRATCH, SCRATCH_DIMM_NUM);
719 val = pci_read_config8(MEMCTRL, 0xd6);
720 val &= ~0x2;
721 if (regs == 1)
722 val |= 0x2;
723 pci_write_config8(MEMCTRL, 0xd6, val);
724
725 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_MAP);
726 for (val = 0; val < ARRAY_SIZE(ODT_TBL); val += 3) {
727 if (regs == ODT_TBL[val]) {
728 pci_write_config8(MEMCTRL, 0xd8, ODT_TBL[val + 1]);
729 /* Store DRAM & NB ODT setting in d0f4_Rxd8 */
730 pci_write_config8(SCRATCH, SCRATCH_DRAM_NB_ODT, ODT_TBL[val + 2]);
731 break;
732 }
733 }
734
735 pci_write_config8(MEMCTRL, 0xd9, 0x0a);
736 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_NUM);
737 regs--;
738 regs = regs << 1;
739 pci_write_config8(MEMCTRL, 0xe0, DQS_DQ_TBL[regs++]);
740 pci_write_config8(MEMCTRL, 0xe2, DQS_DQ_TBL[regs]);
741
742 /* DRAM Timing CS */
743 pci_write_config8(MEMCTRL, 0xe4, 0x66);
744
745 /* DRAM Timing MAA */
746 val = 0;
747 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
748 if (pci_read_config8(SCRATCH, SCRATCH_REG_BASE + (dimm << 1))) {
749 spds = get_spd_data(&ctrl->cfg, dimm, SPD_PRI_WIDTH);
750 spds = 64 / spds;
751 if (pci_read_config8(SCRATCH, SCRATCH_REG_BASE + (dimm << 1) + 1))
752 spds = spds << 1;
753 val += spds;
754 }
755 }
756 printk(BIOS_DEBUG, "\nchip #%d\n", val);
757 if (val > 18)
758 regs = 0xdb;
759 else
760 regs = 0x86;
761 pci_write_config8(MEMCTRL, 0xe8, regs);
762
763 /* DRAM Timing MAB */
764 pci_write_config8(MEMCTRL, 0xe9, 0x0);
765
766 /* DRAM Timing DCLK VT8454C always 0x66 */
767 pci_write_config8(MEMCTRL, 0xe6, 0xaa);
768
769 /**********************************************/
770 /* Set DRAM Duty Control */
771 /**********************************************/
772 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_NUM);
773 switch (regs) {
774 case 1:
775 case 2: /* 1~2 rank */
776 val = 0;
777 break;
778 case 3:
779 case 4: /* 3~4 rank */
780 regs = pci_read_config8(SCRATCH, SCRATCH_DRAM_FREQ);
781 if (regs == DDRII_533)
782 val = 4;
783 else /* DDRII-400 */
784 val = 0;
785 break;
786 }
787 regs = 0xec;
788 for (t = 0; t < 4; t++) {
789 pci_write_config8(MEMCTRL, regs, Duty_Control_DDR2[val]);
790 regs++;
791 val++;
792 }
793
794 /**********************************************/
795 /* Set DRAM Clock Control */
796 /**********************************************/
797 /* Write Data Phase */
798 val = pci_read_config8(SCRATCH, SCRATCH_DRAM_FREQ);
799 regs = pci_read_config8(MEMCTRL, 0x75);
800 regs &= 0xf0;
801 switch (val) {
802 case DDRII_533:
803 pci_write_config8(MEMCTRL, 0x74, 0x07);
804 regs |= 0x7;
805 break;
806 case DDRII_400:
807 default:
808 pci_write_config8(MEMCTRL, 0x74, 0x05);
809 regs |= 0x5;
810 break;
811 }
812 pci_write_config8(MEMCTRL, 0x75, regs);
813 pci_write_config8(MEMCTRL, 0x76, 0x80);
814
815 /* Clock Phase Control for FeedBack Mode */
816 regs = pci_read_config8(MEMCTRL, 0x90);
817 pci_write_config8(MEMCTRL, 0x90, regs);
818
819 regs = pci_read_config8(SCRATCH, SCRATCH_DRAM_FREQ);
820 switch (regs) {
821 case DDRII_533:
822 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_NUM);
823 if (regs == 1)
824 val = 0;
825 else
826 val = 3;
827 break;
828 case DDRII_400:
829 default:
830 val = 6;
831 break;
832 }
833 regs = pci_read_config8(MEMCTRL, 0x91);
834 regs &= ~0xc0;
835 regs |= ctrl->cfg.mem_clocks << 6;
836 pci_write_config8(MEMCTRL, 0x91, regs);
837 regs = 0x91;
838 for (t = 0; t < 3; t++) {
839 dimm = pci_read_config8(MEMCTRL, regs);
840 dimm &= ~0x7;
841 dimm |= ChA_Clk_Phase_DDR2_Table[val];
842 pci_write_config8(MEMCTRL, regs, dimm);
843 regs++;
844 val++;
845 }
846
847 pci_write_config8(MEMCTRL, 0x97, 0x12);
848 pci_write_config8(MEMCTRL, 0x98, 0x33);
849
850 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_0);
851 val = pci_read_config8(SCRATCH, SCRATCH_RANK_2);
852 if (regs && val)
853 pci_write_config8(MEMCTRL, 0x9d, 0x00);
854 else
855 pci_write_config8(MEMCTRL, 0x9d, 0x0f);
856
857 for (val = 0; val < ARRAY_SIZE(DQ_DQS_Table); val += 2)
858 pci_write_config8(MEMCTRL, DQ_DQS_Table[val], DQ_DQS_Table[val + 1]);
859 regs = pci_read_config8(SCRATCH, SCRATCH_DRAM_FREQ);
860 if (regs == DDRII_533)
861 pci_write_config8(MEMCTRL, 0x7b, 0xa0);
862 else
863 pci_write_config8(MEMCTRL, 0x7b, 0x10);
864
865 /***************************************************/
866 /* Set necessary register before DRAM initialize */
867 /***************************************************/
868 for (val = 0; val < ARRAY_SIZE(Mem_Reg_Init); val += 3) {
869 regs = pci_read_config8(MEMCTRL, Mem_Reg_Init[val]);
870 regs &= Mem_Reg_Init[val + 1];
871 regs |= Mem_Reg_Init[val + 2];
872 pci_write_config8(MEMCTRL, Mem_Reg_Init[val], regs);
873 }
874 regs = pci_read_config8(HOSTCTRL, 0x51);
875 regs &= 0xbf; // Clear bit 6 Disable Read Around Write
876 pci_write_config8(HOSTCTRL, 0x51, regs);
877
878 regs = pci_read_config8(HOSTCTRL, 0x54);
879 t = regs >> 5;
880 val = pci_read_config8(HOSTCTRL, 0x57);
881 dimm = val >> 5;
882 if (t == dimm)
883 t = 0x0;
884 else
885 t = 0x1;
886 regs &= ~0x1;
887 regs |= t;
888 val &= ~0x1;
889 val |= t;
890 pci_write_config8(HOSTCTRL, 0x57, val);
891
892 regs = pci_read_config8(HOSTCTRL, 0x51);
893 regs |= t;
894 pci_write_config8(HOSTCTRL, 0x51, regs);
895
896 regs = pci_read_config8(MEMCTRL, 0x90);
897 regs &= 0x7;
898 val = 0;
899 if (regs < 0x2)
900 val = 0x80;
901 regs = pci_read_config8(MEMCTRL, 0x76);
902 regs &= 0x80;
903 regs |= val;
904 pci_write_config8(MEMCTRL, 0x76, regs);
905
906 regs = pci_read_config8(MEMCTRL, 0x6f);
907 regs |= 0x10;
908 pci_write_config8(MEMCTRL, 0x6f, regs);
909
910 /***************************************************/
911 /* Find suitable DQS value for ChA and ChB */
912 /***************************************************/
913 // Set DQS output delay for Channel A
914 regs = pci_read_config8(SCRATCH, SCRATCH_DRAM_FREQ);
915 val = pci_read_config8(SCRATCH, SCRATCH_RANK_NUM);
916 switch (regs) {
917 case DDRII_533:
918 if (val <= 2)
919 val = 0;
920 else
921 val = 2;
922 break;
923 case DDRII_400:
924 default:
925 if (val <= 2)
926 val = 4;
927 else
928 val = 6;
929 break;
930 }
931 for (t = 0; t < 2; t++)
932 pci_write_config8(MEMCTRL, (0x70 + t), DQSOChA_DDR2_Driving_Table[val + t]);
933 // Set DQS output delay for Channel B
934 pci_write_config8(MEMCTRL, 0x72, 0x0);
935
936 regs = pci_read_config8(SCRATCH, SCRATCH_RANK_0);
937 val = pci_read_config8(SCRATCH, SCRATCH_RANK_2);
938 if (regs && val)
939 pci_write_config8(MEMCTRL, 0x73, 0xfd);
940 else
941 pci_write_config8(MEMCTRL, 0x73, 0x01);
942 }
943
944 static void sdram_set_registers(const struct mem_controller *ctrl)
945 {
946 c7_cpu_setup(ctrl);
947 ddr_detect(ctrl);
948 sdram_set_safe_values(ctrl);
949 }
950
951 static void step_20_21(const struct mem_controller *ctrl)
952 {
953 u8 val;
954
955 // Step 20
956 udelay(200);
957
958 val = pci_read_config8(SCRATCH, SCRATCH_DRAM_NB_ODT);
959 if (val & DDR2_ODT_150ohm)
960 read32p(0x102200);
961 else
962 read32p(0x102020);
963
964 /* Step 21. Normal operation */
965 printk(BIOS_SPEW, "RAM Enable 5: Normal operation\n");
966 do_ram_command(ctrl, RAM_COMMAND_NORMAL);
967 udelay(3);
968 }
969
970 static void step_2_19(const struct mem_controller *ctrl)
971 {
972 u32 i;
973 u8 val;
974
975 // Step 2
976 val = pci_read_config8(MEMCTRL, 0x69);
977 val &= ~0x03;
978 pci_write_config8(MEMCTRL, 0x69, val);
979
980 /* Step 3 Apply NOP. */
981 printk(BIOS_SPEW, "RAM Enable 1: Apply NOP\n");
982 do_ram_command(ctrl, RAM_COMMAND_NOP);
983
984 udelay(15);
985
986 // Step 4
987 printk(BIOS_SPEW, "SEND: ");
988 read32p(0);
989 printk(BIOS_SPEW, "OK\n");
990
991 // Step 5
992 udelay(400);
993
994 /* 6. Precharge all. Wait tRP. */
995 printk(BIOS_SPEW, "RAM Enable 2: Precharge all\n");
996 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
997
998 // Step 7
999 printk(BIOS_SPEW, "SEND: ");
1000 read32p(0);
1001 printk(BIOS_SPEW, "OK\n");
1002
1003 /* Step 8. Mode register set. */
1004 printk(BIOS_SPEW, "RAM Enable 4: Mode register set\n");
1005 do_ram_command(ctrl, RAM_COMMAND_MRS); //enable dll
1006
1007 // Step 9
1008 printk(BIOS_SPEW, "SEND: ");
1009
1010 val = pci_read_config8(SCRATCH, SCRATCH_DRAM_NB_ODT);
1011 if (val & DDR2_ODT_150ohm)
1012 read32p(0x102200); //DDR2_ODT_150ohm
1013 else
1014 read32p(0x102020);
1015 printk(BIOS_SPEW, "OK\n");
1016
1017 // Step 10
1018 printk(BIOS_SPEW, "SEND: ");
1019 read32p(0x800);
1020 printk(BIOS_SPEW, "OK\n");
1021
1022 /* Step 11. Precharge all. Wait tRP. */
1023 printk(BIOS_SPEW, "RAM Enable 2: Precharge all\n");
1024 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
1025
1026 // Step 12
1027 printk(BIOS_SPEW, "SEND: ");
1028 read32p(0);
1029 printk(BIOS_SPEW, "OK\n");
1030
1031 /* Step 13. Perform 8 refresh cycles. Wait tRC each time. */
1032 printk(BIOS_SPEW, "RAM Enable 3: CBR\n");
1033 do_ram_command(ctrl, RAM_COMMAND_CBR);
1034
1035 /* JEDEC says only twice, do 8 times for posterity */
1036 // Step 16: Repeat Step 14 and 15 another 7 times
1037 for (i = 0; i < 8; i++) {
1038 // Step 14
1039 read32p(0);
1040 printk(BIOS_SPEW, ".");
1041
1042 // Step 15
1043 udelay(100);
1044 }
1045
1046 /* Step 17. Mode register set. Wait 200us. */
1047 printk(BIOS_SPEW, "\nRAM Enable 4: Mode register set\n");
1048
1049 //safe value for now, BL = 8, WR = 4, CAS = 4
1050 do_ram_command(ctrl, RAM_COMMAND_MRS);
1051 udelay(200);
1052
1053 /* Use Single Chanel temporarily */
1054 val = pci_read_config8(MEMCTRL, 0x6c);
1055 if (val & 0x8) { /* Burst Length = 8 */
1056 val = pci_read_config8(MEMCTRL, 0x62);
1057 val &= 0x7;
1058 i = DDR2_MRS_table[4 + val];
1059 } else {
1060 val = pci_read_config8(MEMCTRL, 0x62);
1061 val &= 0x7;
1062 i = DDR2_MRS_table[val];
1063 }
1064
1065 // Step 18
1066 val = pci_read_config8(MEMCTRL, 0x61);
1067 val = val >> 6;
1068 i |= DDR2_Twr_table[val];
1069 read32p(i);
1070
1071 printk(BIOS_DEBUG, "MRS = %08x\n", i);
1072
1073 udelay(15);
1074
1075 // Step 19
1076 val = pci_read_config8(SCRATCH, SCRATCH_DRAM_NB_ODT);
1077 if (val & DDR2_ODT_150ohm)
1078 read32p(0x103e00); //EMRS OCD Default
1079 else
1080 read32p(0x103c20);
1081 }
1082
1083 static void sdram_set_vr(const struct mem_controller *ctrl, u8 num)
1084 {
1085 u8 reg, val;
1086 val = 0x54 + (num >> 1);
1087 reg = pci_read_config8(MEMCTRL, val);
1088 reg &= (0xf << (4 * (num & 0x1)));
1089 reg |= (((0x8 | num) << 4) >> (4 * (num & 0x1)));
1090 pci_write_config8(MEMCTRL, val, reg);
1091 }
1092 static void sdram_ending_addr(const struct mem_controller *ctrl, u8 num)
1093 {
1094 u8 reg, val;
1095 /* Set Ending Address */
1096 val = 0x40 + num;
1097 reg = pci_read_config8(MEMCTRL, val);
1098 reg += 0x10;
1099 pci_write_config8(MEMCTRL, val, reg);
1100 /* Set Beginning Address */
1101 val = 0x48 + num;
1102 pci_write_config8(MEMCTRL, val, 0x0);
1103 }
1104
1105 static void sdram_clear_vr_addr(const struct mem_controller *ctrl, u8 num)
1106 {
1107 u8 reg, val;
1108 val = 0x54 + (num >> 1);
1109 reg = pci_read_config8(MEMCTRL, val);
1110 reg &= ~(0x80 >> (4 * (num & 0x1)));
1111 pci_write_config8(MEMCTRL, val, reg);
1112 val = 0x40 + num;
1113 reg = pci_read_config8(MEMCTRL, val);
1114 reg -= 0x10;
1115 pci_write_config8(MEMCTRL, val, reg);
1116 val = 0x48 + num;
1117 pci_write_config8(MEMCTRL, val, 0x0);
1118 }
1119
1120 /* Perform sizing DRAM by dynamic method */
1121 static void sdram_calc_size(const struct mem_controller *ctrl, u8 num)
1122 {
1123 u8 ca, ra, ba, reg;
1124 ba = pci_read_config8(SCRATCH, SCRATCH_FLAGS);
1125 if (ba == 8) {
1126 write8p(0, 0x0d);
1127 ra = read8p(0);
1128 write8p(1 << SDRAM1X_RA_12_8bk, 0x0c);
1129 ra = read8p(0);
1130
1131 write8p(0, 0x0a);
1132 ca = read8p(0);
1133 write8p(1 << SDRAM1X_CA_09_8bk, 0x0c);
1134 ca = read8p(0);
1135
1136 write8p(0, 0x03);
1137 ba = read8p(0);
1138 write8p(1 << SDRAM1X_BA2_8bk, 0x02);
1139 ba = read8p(0);
1140 write8p(1 << SDRAM1X_BA1_8bk, 0x01);
1141 ba = read8p(0);
1142 } else {
1143 write8p(0, 0x0f);
1144 ra = read8p(0);
1145 write8p(1 << SDRAM1X_RA_14, 0x0e);
1146 ra = read8p(0);
1147 write8p(1 << SDRAM1X_RA_13, 0x0d);
1148 ra = read8p(0);
1149 write8p(1 << SDRAM1X_RA_12, 0x0c);
1150 ra = read8p(0);
1151
1152 write8p(0, 0x0c);
1153 ca = read8p(0);
1154 write8p(1 << SDRAM1X_CA_12, 0x0b);
1155 ca = read8p(0);
1156 write8p(1 << SDRAM1X_CA_11, 0x0a);
1157 ca = read8p(0);
1158 write8p(1 << SDRAM1X_CA_09, 0x09);
1159 ca = read8p(0);
1160
1161 write8p(0, 0x02);
1162 ba = read8p(0);
1163 write8p(1 << SDRAM1X_BA1, 0x01);
1164 ba = read8p(0);
1165 }
1166
1167 if (ra < 10 || ra > 15)
1168 die("bad RA %u\n", ra);
1169 if (ca < 8 || ca > 12)
1170 die("bad CA %u\n", ca);
1171 if (ba < 1 || ba > 3)
1172 die("bad BA %u\n", ba);
1173
1174 /* Calculate MA type save to scratch register */
1175 reg = 0;
1176
1177 switch (ra) {
1178 case 12:
1179 reg |= MA_12_Row;
1180 break;
1181 case 13:
1182 reg |= MA_13_Row;
1183 break;
1184 case 14:
1185 reg |= MA_14_Row;
1186 break;
1187 default:
1188 reg |= MA_15_Row;
1189 }
1190
1191 switch (ca) {
1192 case 9:
1193 reg |= MA_9_Column;
1194 break;
1195 case 10:
1196 reg |= MA_10_Column;
1197 break;
1198 case 11:
1199 reg |= MA_11_Column;
1200 break;
1201 default:
1202 reg |= MA_12_Column;
1203 }
1204
1205 switch (ba) {
1206 case 3:
1207 reg |= MA_8_Bank;
1208 break;
1209 default:
1210 reg |= MA_4_Bank;
1211 }
1212
1213 pci_write_config8(SCRATCH, SCRATCH_RANK0_MA_REG + num, reg);
1214
1215 if (ra >= 13)
1216 pci_write_config8(SCRATCH, SCRATCH_DRAM_256M_BIT, 1);
1217
1218 /* Calculate rank size save to scratch register */
1219 ra = ra + ca + ba + 3 - 26; /* 1 unit = 64M */
1220 ra = 1 << ra;
1221 pci_write_config8(SCRATCH, SCRATCH_RANK0_SIZE_REG + num, ra);
1222 }
1223
1224 static void enable_shadow_ram(const struct mem_controller *ctrl)
1225 {
1226 u8 shadowreg;
1227
1228 pci_write_config8(MEMCTRL, 0x80, 0x2a);
1229
1230 /* 0xf0000-0xfffff - ACPI tables */
1231 shadowreg = pci_read_config8(MEMCTRL, 0x83);
1232 shadowreg |= 0x30;
1233 pci_write_config8(MEMCTRL, 0x83, shadowreg);
1234 }
1235
1236 void sdram_enable(const struct dram_cfg *cfg)
1237 {
1238 struct mem_controller ctrl = { *cfg, };
1239 u8 reg8;
1240 u8 val, i;
1241 u8 dl, dh;
1242 u32 quot;
1243
1244 sdram_set_registers(&ctrl);
1245 enable_shadow_ram(&ctrl);
1246
1247 /* Init Present Bank */
1248 for (i = 0; i < ARRAY_SIZE(Init_Rank_Reg_Table); i++)
1249 pci_write_config8(MEMCTRL, Init_Rank_Reg_Table[i], 0x0);
1250
1251 /* Init other banks */
1252 for (i = 0; i < 4; i++) {
1253 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1254 if (reg8) {
1255 sdram_set_vr(&ctrl, i);
1256 sdram_ending_addr(&ctrl, i);
1257 step_2_19(&ctrl);
1258 step_20_21(&ctrl);
1259 sdram_clear_vr_addr(&ctrl, i);
1260 }
1261 }
1262
1263 /****************************************************************/
1264 /* Find the DQSI Low/High bound and save it to Scratch register */
1265 /****************************************************************/
1266 for (dl = 0; dl < 0x3f; dl += 2) {
1267 reg8 = dl & 0x3f;
1268 reg8 |= 0x80; /* Set Manual Mode */
1269 pci_write_config8(MEMCTRL, 0x77, reg8);
1270 for (i = 0; i < 4; i++) {
1271 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1272 if (reg8) {
1273 sdram_set_vr(&ctrl, i);
1274 sdram_ending_addr(&ctrl, i);
1275 write32p(0, 0x55555555);
1276 write32p(4, 0x55555555);
1277 udelay(15);
1278 if (read32p(0) != 0x55555555)
1279 break;
1280 if (read32p(4) != 0x55555555)
1281 break;
1282 write32p(0, 0xaaaaaaaa);
1283 write32p(4, 0xaaaaaaaa);
1284 udelay(15);
1285 if (read32p(0) != 0xaaaaaaaa)
1286 break;
1287 if (read32p(4) != 0xaaaaaaaa)
1288 break;
1289 sdram_clear_vr_addr(&ctrl, i);
1290 }
1291 }
1292 if (i == 4)
1293 break;
1294 else
1295 sdram_clear_vr_addr(&ctrl, i);
1296 }
1297 printk(BIOS_DEBUG, "DQSI Low 0x%02x\n", dl);
1298 for (dh = dl; dh < 0x3f; dh += 2) {
1299 reg8 = dh & 0x3f;
1300 reg8 |= 0x80; /* Set Manual Mode */
1301 pci_write_config8(MEMCTRL, 0x77, reg8);
1302 for (i = 0; i < 4; i++) {
1303 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1304 if (reg8) {
1305 sdram_set_vr(&ctrl, i);
1306 sdram_ending_addr(&ctrl, i);
1307
1308 write32p(0, 0x55555555);
1309 write32p(4, 0x55555555);
1310 udelay(15);
1311 if (read32p(0) != 0x55555555)
1312 break;
1313 if (read32p(4) != 0x55555555)
1314 break;
1315 write32p(0, 0xaaaaaaaa);
1316 write32p(4, 0xaaaaaaaa);
1317 udelay(15);
1318 if (read32p(0) != 0xaaaaaaaa)
1319 break;
1320 if (read32p(4) != 0xaaaaaaaa)
1321 break;
1322 sdram_clear_vr_addr(&ctrl, i);
1323 }
1324 }
1325 if (i != 4) {
1326 sdram_clear_vr_addr(&ctrl, i);
1327 break;
1328 }
1329 }
1330 printk(BIOS_DEBUG, "DQSI High 0x%02x\n", dh);
1331 pci_write_config8(SCRATCH, SCRATCH_CHA_DQSI_LOW_REG, dl);
1332 pci_write_config8(SCRATCH, SCRATCH_CHA_DQSI_HIGH_REG, dh);
1333 reg8 = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1334 val = DQSI_Rate_Table[reg8];
1335 quot = dh - dl;
1336 quot = quot * val;
1337 quot >>= 4;
1338 val = quot + dl;
1339 pci_write_config8(SCRATCH, SCRATCH_ChA_DQSI_REG, val);
1340 reg8 = val & 0x3f;
1341 reg8 |= 0x80;
1342 pci_write_config8(MEMCTRL, 0x77, reg8);
1343
1344 /****************************************************************/
1345 /* Find out the lowest Bank Interleave and Set Register */
1346 /****************************************************************/
1347 dl = 2;
1348 for (i = 0; i < 4; i++) {
1349 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1350 if (reg8) {
1351 reg8 = get_spd_data(&ctrl.cfg, (i >> 1), 17);
1352 sdram_set_vr(&ctrl, i);
1353 sdram_ending_addr(&ctrl, i);
1354 if (reg8 == 4) {
1355 write8p(0, 0x02);
1356 val = read8p(0);
1357 write8p(1 << SDRAM1X_BA1, 0x01);
1358 val = read8p(0);
1359 } else {
1360 write8p(0, 0x03);
1361 val = read8p(0);
1362 write8p(1 << SDRAM1X_BA2_8bk, 0x02);
1363 val = read8p(0);
1364 write8p(1 << SDRAM1X_BA1_8bk, 0x01);
1365 val = read8p(0);
1366 }
1367 if (val < dl)
1368 dl = val;
1369 sdram_clear_vr_addr(&ctrl, i);
1370 }
1371 }
1372 dl <<= 6;
1373 reg8 = pci_read_config8(MEMCTRL, 0x69);
1374 reg8 &= ~0xc0;
1375 reg8 |= dl;
1376 pci_write_config8(MEMCTRL, 0x69, reg8);
1377
1378 /****************************************************************/
1379 /* DRAM Sizing and Fill MA type */
1380 /****************************************************************/
1381 for (i = 0; i < 4; i++) {
1382 val = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1383 if (val) {
1384 reg8 = get_spd_data(&ctrl.cfg, (i >> 1), 17);
1385 pci_write_config8(SCRATCH, SCRATCH_FLAGS, reg8);
1386 if (reg8 == 4) {
1387 /* Use MA Type 3 for DRAM sizing */
1388 reg8 = pci_read_config8(MEMCTRL, 0x50);
1389 reg8 &= 0x11;
1390 reg8 |= 0x66;
1391 pci_write_config8(MEMCTRL, 0x50, reg8);
1392 pci_write_config8(MEMCTRL, 0x51, reg8);
1393 } else {
1394 /* Use MA Type 5 for DRAM sizing */
1395 reg8 = pci_read_config8(MEMCTRL, 0x50);
1396 reg8 &= 0x11;
1397 reg8 |= 0xaa;
1398 pci_write_config8(MEMCTRL, 0x50, reg8);
1399 pci_write_config8(MEMCTRL, 0x51, reg8);
1400 reg8 = pci_read_config8(MEMCTRL, 0x53);
1401 reg8 &= 0x0f;
1402 reg8 |= 0x90;
1403 pci_write_config8(MEMCTRL, 0x53, reg8);
1404 }
1405 sdram_set_vr(&ctrl, i);
1406 val = 0x40 + i;
1407 reg8 = pci_read_config8(MEMCTRL, val);
1408 /* max size 3G for new MA table */
1409 reg8 += 0x30;
1410 pci_write_config8(MEMCTRL, val, reg8);
1411 /* Set Beginning Address */
1412 val = 0x48 + i;
1413 pci_write_config8(MEMCTRL, val, 0x0);
1414
1415 sdram_calc_size(&ctrl, i);
1416
1417 /* Clear */
1418 val = 0x54 + (i >> 1);
1419 reg8 = pci_read_config8(MEMCTRL, val);
1420 reg8 = ~(0x80 >> (4 * (i & 0x1)));
1421 pci_write_config8(MEMCTRL, val, reg8);
1422 val = 0x40 + i;
1423 reg8 = pci_read_config8(MEMCTRL, val);
1424 reg8 -= 0x30;
1425 pci_write_config8(MEMCTRL, val, reg8);
1426 val = 0x48 + i;
1427 pci_write_config8(MEMCTRL, val, 0x0);
1428
1429 }
1430 }
1431 /* Clear MA Type */
1432 reg8 = pci_read_config8(MEMCTRL, 0x50);
1433 reg8 &= 0x11;
1434 pci_write_config8(MEMCTRL, 0x50, reg8);
1435 pci_write_config8(MEMCTRL, 0x51, reg8);
1436 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1437 reg8 &= ~0x08;
1438 pci_write_config8(MEMCTRL, 0x6b, reg8);
1439
1440 /****************************************************************/
1441 /* DRAM re-initialize for burst length */
1442 /****************************************************************/
1443 for (i = 0; i < 4; i++) {
1444 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1445 if (reg8) {
1446 sdram_set_vr(&ctrl, i);
1447 sdram_ending_addr(&ctrl, i);
1448 step_2_19(&ctrl);
1449 step_20_21(&ctrl);
1450 sdram_clear_vr_addr(&ctrl, i);
1451 }
1452 }
1453
1454 /****************************************************************/
1455 /* Set the MA Type */
1456 /****************************************************************/
1457 reg8 = pci_read_config8(MEMCTRL, 0x50);
1458 reg8 &= 0x11;
1459 pci_write_config8(MEMCTRL, 0x50, reg8);
1460
1461 reg8 = pci_read_config8(MEMCTRL, 0x51);
1462 reg8 &= 0x11;
1463 pci_write_config8(MEMCTRL, 0x51, reg8);
1464
1465 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1466 reg8 &= ~0x08;
1467 pci_write_config8(MEMCTRL, 0x6b, reg8);
1468
1469 for (i = 0; i < 4; i += 2) {
1470 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1471 if (reg8) {
1472 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK0_MA_REG + i);
1473 reg8 &= (MA_Bank + MA_Column);
1474 val = pci_read_config8(MEMCTRL, 0x50);
1475 if (i == 0) {
1476 reg8 <<= 4;
1477 val &= 0x1f;
1478 } else
1479 val &= 0xf1;
1480 val |= reg8;
1481 pci_write_config8(MEMCTRL, 0x50, val);
1482 }
1483 }
1484
1485 /****************************************************************/
1486 /* Set Start and Ending Address */
1487 /****************************************************************/
1488 dl = 0; /* Begin Address */
1489 dh = 0; /* Ending Address */
1490 for (i = 0; i < 4; i++) {
1491 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1492 if (reg8) {
1493 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK0_SIZE_REG + i);
1494 if (reg8 == 0)
1495 continue;
1496 dh += reg8;
1497 pci_write_config8(MEMCTRL, (0x40 + i), dh);
1498 pci_write_config8(MEMCTRL, (0x48 + i), dl);
1499 dl = dh;
1500 }
1501 }
1502 dh <<= 2;
1503 // F7_Rx57 Ending address mirror register
1504 pci_write_config8(_sdev_north_end, 0x57, dh);
1505 pci_write_config8(_sdev_south_end, 0x57, dh);
1506 // LOW TOP Address
1507 pci_write_config8(MEMCTRL, 0x88, dh);
1508 pci_write_config8(MEMCTRL, 0x85, dh);
1509 // also program vlink mirror
1510 pci_write_config8(_sdev_north_end, 0xe5, dh);
1511
1512 /****************************************************************/
1513 /* Set Physical to Virtual Rank mapping */
1514 /****************************************************************/
1515 pci_write_config32(MEMCTRL, 0x54, 0x0);
1516 for (i = 0; i < 4; i++) {
1517 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1518 if (reg8) {
1519 reg8 = pci_read_config8(MEMCTRL, (0x54 + (i >> 1)));
1520 if (i & 0x1) { /* Odd Rank */
1521 reg8 &= 0xf0;
1522 reg8 |= (0x8 | i);
1523 } else { /* Even Rank */
1524
1525 reg8 &= 0x0f;
1526 reg8 |= ((0x8 | i) << 4);
1527 }
1528 pci_write_config8(MEMCTRL, (0x54 + (i >> 1)), reg8);
1529 }
1530 }
1531
1532 /****************************************************************/
1533 /* Set DRAM Refresh Counter */
1534 /****************************************************************/
1535 val = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1536 val <<= 1;
1537 reg8 = pci_read_config8(SCRATCH, SCRATCH_DRAM_256M_BIT);
1538 if (reg8)
1539 val++;
1540 pci_write_config8(MEMCTRL, 0x6a, REFC_Table[val]);
1541
1542 /****************************************************************/
1543 /* Chipset Performance UP and other setting after DRAM Sizing */
1544 /****************************************************************/
1545 /* Dram Registers */
1546 for (i = 0; i < ARRAY_SIZE(Dram_Table); i += 3) {
1547 reg8 = pci_read_config8(MEMCTRL, Dram_Table[i]);
1548 reg8 &= Dram_Table[i + 1];
1549 reg8 |= Dram_Table[i + 2];
1550 pci_write_config8(MEMCTRL, Dram_Table[i], reg8);
1551 }
1552
1553 /* Host Registers */
1554 for (i = 0; i < ARRAY_SIZE(Host_Table); i += 3) {
1555 reg8 = pci_read_config8(HOSTCTRL, Host_Table[i]);
1556 reg8 &= Host_Table[i + 1];
1557 reg8 |= Host_Table[i + 2];
1558 pci_write_config8(HOSTCTRL, Host_Table[i], reg8);
1559 }
1560
1561 pci_write_config8(HOSTCTRL, 0x5d, 0xb2);
1562
1563 /****************************************************************/
1564 /* UMA registers for N-series projects */
1565 /****************************************************************/
1566
1567 /* Manual setting frame buffer bank */
1568 for (i = 0; i < 4; i++) {
1569 reg8 = pci_read_config8(SCRATCH, SCRATCH_RANK_0 + i);
1570 if (reg8)
1571 val = i;
1572 }
1573 pci_write_config8(MEMCTRL, 0xb0, val);
1574 reg8 = 0x40; // Frame buffer size 64M
1575 reg8 |= 0x80; // VGA Enable
1576 reg8 |= 0x0a; // A[31:28] = 1010b
1577 pci_write_config8(MEMCTRL, 0xa1, reg8);
1578
1579 static const struct regmask {
1580 u8 reg;
1581 u8 mask;
1582 u8 val;
1583 } b0d1f0[] = {
1584 { 0x40, 0x00, 0x8b},
1585 { 0x41, 0x80, 0x43},
1586 { 0x42, 0x00, 0x62},
1587 { 0x43, 0x00, 0x44},
1588 { 0x44, 0x00, 0x34},
1589 { 0x45, 0x00, 0x72}
1590 }, b0d0f3[] = {
1591 { 0x53, 0xf0, 0x0f},
1592 { 0x60, 0x00, 0x03},
1593 { 0x65, 0x00, 0xd9},
1594 { 0x66, 0x00, 0x80},
1595 { 0x67, 0x00, 0x00},
1596 { 0x68, 0x00, 0x01},
1597 { 0x69, 0xe0, 0x03},
1598 { 0x6b, 0x00, 0x10},
1599 { 0x6c, 0xc1, 0x08},
1600 { 0x6e, 0x00, 0x89},
1601 { 0x6f, 0x00, 0x51},
1602 { 0x75, ~0x40, 0x40},
1603 { 0x76, 0x8f, 0x00},
1604 { 0x7b, 0x00, 0xa0},
1605 { 0x86, 0x01, 0x24},
1606 { 0x86, 0x04, 0x29},
1607 { 0x8c, 0x00, 0x00},
1608 { 0x8d, 0x00, 0x00},
1609 { 0x95, ~0x40, 0x00},
1610 { 0xa2, 0x00, 0x44},
1611 { 0xb1, 0x00, 0xaa}
1612 }, b0d0f0[] = {
1613 { 0x4d, 0x00, 0x24},
1614 { 0x4f, 0x00, 0x01},
1615 { 0xbc, 0x00, 0x21},
1616 { 0xbe, 0x00, 0x00},
1617 { 0xbf, 0x7f, 0x80}
1618 }, b0d17f0[] = {
1619 { 0x40, ~0x01, 0x01}, // enable timer/counter shadow registers
1620 { 0x67, ~0x03, 0x01},
1621 { 0x5b, ~0x01, 0x00},
1622 { 0x8d, ~0x02, 0x02},
1623 { 0x97, 0x7f, 0x00},
1624 { 0xd2, ~0x18, 0x00},
1625 { 0xe2, ~0x36, 0x06},
1626 { 0xe4, 0x7f, 0x00},
1627 { 0xe5, 0x00, 0x40},
1628 { 0xe6, 0x00, 0x20},
1629 { 0xe7, 0x2f, 0xc0},
1630 { 0xec, ~0x08, 0x00}
1631 }, b0d17f7[] = {
1632 { 0x4e, 0x7f, 0x80},
1633 { 0x4f, ~(1 << 6), 1 << 6 }, /* PG_CX700: 14.1.1 enable P2P Bridge Header for External PCI Bus */
1634 { 0x74, ~0x00, 0x04}, /* PG_CX700: 14.1.2 APIC FSB directly up to snmic, not on pci */
1635 { 0x7c, ~0x00, 0x02}, /* PG_CX700: 14.1.1 APIC FSB directly up to snmic, not on pci */
1636 { 0xe6, 0x0, 0x04} // MSI post
1637 }, b0d19f0[] = { /* P2PE */
1638 { 0x42, ~0x08, 0x08}, // Disable HD Audio,
1639 { 0x40, 0x3f, 0x80} // 14.1.3.1.1 of the PG: extended cfg mode for pcie. enable capability, but don't activate
1640 }, b0d0f2[] = {
1641 { 0x50, ~0x40, 0x88},
1642 { 0x51, 0x80, 0x7b},
1643 { 0x52, 0x90, 0x6f},
1644 { 0x53, 0x00, 0x88},
1645 { 0x54, 0xe4, 0x16},
1646 { 0x55, 0xf2, 0x04},
1647 { 0x56, 0x0f, 0x00},
1648 { 0x57, ~0x04, 0x00},
1649 { 0x5d, 0x00, 0xb2},
1650 { 0x5e, 0x00, 0x88},
1651 { 0x5f, 0x00, 0xc7},
1652 { 0x5c, 0x00, 0x01}
1653 };
1654
1655 REGISTERPRESET(0, 0, 0, b0d0f0);
1656 REGISTERPRESET(0, 0, 2, b0d0f2);
1657 REGISTERPRESET(0, 0, 3, b0d0f3);
1658 REGISTERPRESET(0, 1, 0, b0d1f0);
1659 REGISTERPRESET(0, 17, 0, b0d17f0);
1660 REGISTERPRESET(0, 17, 7, b0d17f7);
1661 REGISTERPRESET(0, 19, 0, b0d19f0);
1662 }