2 * This file is part of the coreboot project.
4 * Copyright (C) 2007 Advanced Micro Devices, Inc.
5 * Copyright (C) 2010 Nils Jacobs
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <cpu/amd/gx2def.h>
24 static const unsigned char NumColAddr[] = {
25 0x00, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x07,
26 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
29 static void banner(const char *s)
31 printk(BIOS_DEBUG, " * %s\n", s);
37 /* this guarantees we flush the UART fifos (if any) and also
38 * ensures that things, in general, keep going so no debug output
45 static void auto_size_dimm(unsigned int dimm)
47 uint32_t dimm_setting;
54 banner("Check present");
55 /* Check that we have a dimm */
56 if (spd_read_byte(dimm, SPD_MEMORY_TYPE) == 0xFF) {
61 /* Field: Module Banks per DIMM */
62 /* EEPROM byte usage: (5) Number of DIMM Banks */
63 spd_byte = spd_read_byte(dimm, SPD_NUM_DIMM_BANKS);
64 if ((MIN_MOD_BANKS > spd_byte) || (spd_byte > MAX_MOD_BANKS)) {
65 print_emerg("Number of module banks not compatible\n");
66 post_code(ERROR_BANK_SET);
69 dimm_setting |= (spd_byte >> 1) << CF07_UPPER_D0_MB_SHIFT;
72 /* Field: Banks per SDRAM device */
73 /* EEPROM byte usage: (17) Number of Banks on SDRAM Device */
74 spd_byte = spd_read_byte(dimm, SPD_NUM_BANKS_PER_SDRAM);
75 if ((MIN_DEV_BANKS > spd_byte) || (spd_byte > MAX_DEV_BANKS)) {
76 print_emerg("Number of device banks not compatible\n");
77 post_code(ERROR_BANK_SET);
80 dimm_setting |= (spd_byte >> 2) << CF07_UPPER_D0_CB_SHIFT;
84 *; EEPROM byte usage: (3) Number of Row Addresses
85 *; (4) Number of Column Addresses
86 *; (5) Number of DIMM Banks
87 *; (31) Module Bank Density
88 *; Size = Module Density * Module Banks
90 if ((spd_read_byte(dimm, SPD_NUM_ROWS) & 0xF0)
91 || (spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF0)) {
92 print_emerg("Assymetirc DIMM not compatible\n");
93 post_code(ERROR_UNSUPPORTED_DIMM);
96 banner("SPDBANKDENSITY");
98 dimm_size = spd_read_byte(dimm, SPD_BANK_DENSITY);
100 dimm_size |= (dimm_size << 8); /* align so 1GB(bit0) is bit 8, this is a little weird to get gcc to not optimize this out */
101 dimm_size &= 0x01FC; /* and off 2GB DIMM size : not supported and the 1GB size we just moved up to bit 8 as well as all the extra on top */
103 /* Module Density * Module Banks */
104 dimm_size <<= (dimm_setting >> CF07_UPPER_D0_MB_SHIFT) & 1; /* shift to multiply by # DIMM banks */
105 banner("BEFORT CTZ");
106 dimm_size = __builtin_ctz(dimm_size);
107 banner("TEST DIMM SIZE>7");
108 if (dimm_size > 7) { /* 7 is 512MB only support 512MB per DIMM */
109 print_emerg("Only support up to 512MB per DIMM\n");
110 post_code(ERROR_DENSITY_DIMM);
113 dimm_setting |= dimm_size << CF07_UPPER_D0_SZ_SHIFT;
117 *; EEPROM byte usage: (4) Number of Column Addresses
118 *; PageSize = 2^# Column Addresses * Data width in bytes (should be 8bytes for a normal DIMM)
120 *; But this really works by magic.
121 *; If ma[11:0] is the memory address pins, and pa[13:0] is the physical column address
122 *; that MC generates, here is how the MC assigns the pa onto the ma pins:
124 *;ma 11 10 09 08 07 06 05 04 03 02 01 00
125 *;--------------------------------------------------------------------------------------------------------------------------------------
126 *;pa 09 08 07 06 05 04 03 (7 col addr bits = 1K page size)
127 *;pa 10 09 08 07 06 05 04 03 (8 col addr bits = 2K page size)
128 *;pa 11 10 09 08 07 06 05 04 03 (9 col addr bits = 4K page size)
129 *;pa 12 11 10 09 08 07 06 05 04 03 (10 col addr bits = 8K page size)
130 *;pa 13 AP 12 11 10 09 08 07 06 05 04 03 (11 col addr bits = 16K page size)
131 *; *AP=autoprecharge bit
133 *; Remember that pa[2:0] are zeroed out since it's a 64-bit data bus (8 bytes),
134 *; so lower 3 address bits are dont_cares.So from the table above,
135 *; it's easier to see what the old code is doing: if for example,#col_addr_bits=7(06h),
136 *; it adds 3 to get 10, then does 2^10=1K. Get it?*/
138 spd_byte = NumColAddr[spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF];
139 banner("MAXCOLADDR");
140 if (spd_byte > MAX_COL_ADDR) {
141 print_emerg("DIMM page size not compatible\n");
142 post_code(ERROR_SET_PAGE);
145 banner(">11address test");
147 if (spd_byte > 4) { /* if the value is above 4 it means >11 col address lines */
148 spd_byte = 7; /* which means >16k so set to disabled */
150 dimm_setting |= spd_byte << CF07_UPPER_D0_PSZ_SHIFT; /* 0=1k,1=2k,2=4k,etc */
152 banner("RDMSR CF07");
153 msr = rdmsr(MC_CF07_DATA);
154 banner("WRMSR CF07");
156 msr.hi &= 0xFFFF0000;
157 msr.hi |= dimm_setting;
159 msr.hi &= 0x0000FFFF;
160 msr.hi |= dimm_setting << 16;
162 wrmsr(MC_CF07_DATA, msr);
166 static void checkDDRMax(void)
168 uint8_t spd_byte0, spd_byte1;
171 /* PC133 identifier */
172 spd_byte0 = spd_read_byte(DIMM0, SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
173 if (spd_byte0 == 0xFF) {
176 spd_byte1 = spd_read_byte(DIMM1, SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
177 if (spd_byte1 == 0xFF) {
181 /* Use the slowest DIMM */
182 if (spd_byte0 < spd_byte1) {
183 spd_byte0 = spd_byte1;
186 /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
187 speed = 20000 / (((spd_byte0 >> 4) * 10) + (spd_byte0 & 0x0F));
189 /* current speed > max speed? */
190 if (GeodeLinkSpeed() > speed) {
191 print_emerg("DIMM overclocked. Check GeodeLink Speed\n");
192 post_code(POST_PLL_MEM_FAIL);
197 const uint16_t REF_RATE[] = { 15, 3, 7, 31, 62, 125 }; /* ns */
199 static void set_refresh_rate(void)
201 uint8_t spd_byte0, spd_byte1;
202 uint16_t rate0, rate1;
205 spd_byte0 = spd_read_byte(DIMM0, SPD_REFRESH);
210 rate0 = REF_RATE[spd_byte0];
212 spd_byte1 = spd_read_byte(DIMM1, SPD_REFRESH);
217 rate1 = REF_RATE[spd_byte1];
219 /* Use the faster rate (lowest number) */
224 msr = rdmsr(MC_CF07_DATA);
225 msr.lo |= ((rate0 * (GeodeLinkSpeed() / 2)) / 16)
226 << CF07_LOWER_REF_INT_SHIFT;
227 wrmsr(MC_CF07_DATA, msr);
230 const uint8_t CASDDR[] = { 5, 5, 2, 6, 0 }; /* 1(1.5), 1.5, 2, 2.5, 0 */
232 static u8 getcasmap(u32 dimm, u16 glspeed)
235 u8 spd_byte, casmap, casmap_shift=0;
237 /************************** DIMM0 **********************************/
238 casmap = spd_read_byte(dimm, SPD_ACCEPTABLE_CAS_LATENCIES);
239 if (casmap != 0xFF) {
240 /* IF -.5 timing is supported, check -.5 timing > GeodeLink */
241 spd_byte = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_2ND);
243 /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
244 dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
245 if (dimm_speed >= glspeed) {
246 casmap_shift = 1; /* -.5 is a shift of 1 */
247 /* IF -1 timing is supported, check -1 timing > GeodeLink */
248 spd_byte = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_3RD);
250 /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
251 dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
252 if (dimm_speed >= glspeed) {
253 casmap_shift = 2; /* -1 is a shift of 2 */
255 } /* SPD_SDRAM_CYCLE_TIME_3RD (-1) !=0 */
259 } /* SPD_SDRAM_CYCLE_TIME_2ND (-.5) !=0 */
260 /* set the casmap based on the shift to limit possible CAS settings */
261 spd_byte = 31 - __builtin_clz((uint32_t) casmap);
262 /* just want bits in the lower byte since we have to cast to a 32 */
263 casmap &= 0xFF << (spd_byte - casmap_shift);
264 } else { /* No DIMM */
270 static void setCAS(void)
272 /*;*****************************************************************************
275 ;* EEPROM byte usage: (18) SDRAM device attributes - CAS latency
276 ;* EEPROM byte usage: (23) SDRAM Minimum Clock Cycle Time @ CLX -.5
277 ;* EEPROM byte usage: (25) SDRAM Minimum Clock Cycle Time @ CLX -1
279 ;* The CAS setting is based on the information provided in each DIMMs SPD.
280 ;* The speed at which a DIMM can run is described relative to the slowest
281 ;* CAS the DIMM supports. Each speed for the relative CAS settings is
282 ;* checked that it is within the GeodeLink speed. If it isn't within the GeodeLink
283 ;* speed, the CAS setting is removed from the list of good settings for
284 ;* the DIMM. This is done for both DIMMs and the lists are compared to
285 ;* find the lowest common CAS latency setting. If there are no CAS settings
286 ;* in common we out a ERROR_DIFF_DIMMS (78h) to port 80h and halt.
289 ;* Exit: Set fastest CAS Latency based on GeodeLink speed and SPD information.
290 ;* Destroys: We really use everything !
291 ;*****************************************************************************/
293 uint8_t spd_byte, casmap0, casmap1;
296 glspeed = GeodeLinkSpeed();
298 casmap0 = getcasmap(DIMM0, glspeed);
299 casmap1 = getcasmap(DIMM1, glspeed);
301 /********************* CAS_LAT MAP COMPARE ***************************/
303 spd_byte = CASDDR[__builtin_ctz(casmap1)];
304 } else if (casmap1 == 0) {
305 spd_byte = CASDDR[__builtin_ctz(casmap0)];
306 } else if ((casmap0 &= casmap1)) {
307 spd_byte = CASDDR[__builtin_ctz(casmap0)];
309 print_emerg("DIMM CAS Latencies not compatible\n");
310 post_code(ERROR_DIFF_DIMMS);
314 msr = rdmsr(MC_CF8F_DATA);
315 msr.lo &= ~(7 << CF8F_LOWER_CAS_LAT_SHIFT);
316 msr.lo |= spd_byte << CF8F_LOWER_CAS_LAT_SHIFT;
317 wrmsr(MC_CF8F_DATA, msr);
320 static void set_latencies(void)
322 uint32_t memspeed, dimm_setting;
323 uint8_t spd_byte0, spd_byte1;
326 memspeed = GeodeLinkSpeed() / 2;
331 spd_byte0 = spd_read_byte(DIMM0, SPD_tRAS);
332 if (spd_byte0 == 0xFF) {
335 spd_byte1 = spd_read_byte(DIMM1, SPD_tRAS);
336 if (spd_byte1 == 0xFF) {
339 if (spd_byte0 < spd_byte1) {
340 spd_byte0 = spd_byte1;
342 /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
343 spd_byte1 = (spd_byte0 * memspeed) / 1000;
344 if (((spd_byte0 * memspeed) % 1000)) {
350 dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2PRE_SHIFT;
353 spd_byte0 = spd_read_byte(DIMM0, SPD_tRP);
354 if (spd_byte0 == 0xFF) {
357 spd_byte1 = spd_read_byte(DIMM1, SPD_tRP);
358 if (spd_byte1 == 0xFF) {
361 if (spd_byte0 < spd_byte1) {
362 spd_byte0 = spd_byte1;
364 /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
365 spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
366 if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
369 dimm_setting |= spd_byte1 << CF8F_LOWER_PRE2ACT_SHIFT;
372 spd_byte0 = spd_read_byte(DIMM0, SPD_tRCD);
373 if (spd_byte0 == 0xFF) {
376 spd_byte1 = spd_read_byte(DIMM1, SPD_tRCD);
377 if (spd_byte1 == 0xFF) {
380 if (spd_byte0 < spd_byte1) {
381 spd_byte0 = spd_byte1;
383 /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
384 spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
385 if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
388 dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2CMD_SHIFT;
391 spd_byte0 = spd_read_byte(DIMM0, SPD_tRRD);
392 if (spd_byte0 == 0xFF) {
395 spd_byte1 = spd_read_byte(DIMM1, SPD_tRRD);
396 if (spd_byte1 == 0xFF) {
399 if (spd_byte0 < spd_byte1) {
400 spd_byte0 = spd_byte1;
402 /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
403 spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
404 if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
407 dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2ACT_SHIFT;
409 /* tRC = tRP + tRAS */
410 dimm_setting |= (((dimm_setting >> CF8F_LOWER_ACT2PRE_SHIFT) & 0x0F) +
411 ((dimm_setting >> CF8F_LOWER_PRE2ACT_SHIFT) & 0x07))
412 << CF8F_LOWER_REF2ACT_SHIFT;
414 msr = rdmsr(MC_CF8F_DATA);
415 msr.lo &= 0xF00000FF;
416 msr.lo |= dimm_setting;
417 msr.hi |= CF8F_UPPER_REORDER_DIS_SET;
418 wrmsr(MC_CF8F_DATA, msr);
419 printk(BIOS_DEBUG, "MSR MC_CF8F_DATA (%08x) value is %08x:%08x\n",
420 MC_CF8F_DATA, msr.hi, msr.lo);
423 static void set_extended_mode_registers(void)
425 uint8_t spd_byte0, spd_byte1;
427 spd_byte0 = spd_read_byte(DIMM0, SPD_DEVICE_ATTRIBUTES_GENERAL);
428 if (spd_byte0 == 0xFF) {
431 spd_byte1 = spd_read_byte(DIMM1, SPD_DEVICE_ATTRIBUTES_GENERAL);
432 if (spd_byte1 == 0xFF) {
435 spd_byte1 &= spd_byte0;
437 msr = rdmsr(MC_CF07_DATA);
438 if (spd_byte1 & 1) { /* Drive Strength Control */
439 msr.lo |= CF07_LOWER_EMR_DRV_SET;
441 if (spd_byte1 & 2) { /* FET Control */
442 msr.lo |= CF07_LOWER_EMR_QFC_SET;
444 wrmsr(MC_CF07_DATA, msr);
447 static void sdram_set_registers(const struct mem_controller *ctrl)
452 /* Set Refresh Staggering */
453 msrnum = MC_CF07_DATA;
456 msr.lo |= 0x0; /* set refresh to 4SDRAM clocks */
459 /* Memory Interleave: Set HOI here otherwise default is LOI */
460 /* msrnum = MC_CF8F_DATA;
462 msr.hi |= CF8F_UPPER_HOI_LOI_SET;
463 wrmsr(msrnum, msr); */
466 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
470 banner("sdram_set_spd_register");
471 post_code(POST_MEM_SETUP); // post_70h
473 spd_byte = spd_read_byte(DIMM0, SPD_MODULE_ATTRIBUTES);
474 banner("Check DIMM 0");
475 /* Check DIMM is not Register and not Buffered DIMMs. */
476 if ((spd_byte != 0xFF) && (spd_byte & 3)) {
477 print_emerg("DIMM0 NOT COMPATIBLE\n");
478 post_code(ERROR_UNSUPPORTED_DIMM);
481 banner("Check DIMM 1");
482 spd_byte = spd_read_byte(DIMM1, SPD_MODULE_ATTRIBUTES);
483 if ((spd_byte != 0xFF) && (spd_byte & 3)) {
484 print_emerg("DIMM1 NOT COMPATIBLE\n");
485 post_code(ERROR_UNSUPPORTED_DIMM);
489 post_code(POST_MEM_SETUP2); // post_72h
490 banner("Check DDR MAX");
492 /* Check that the memory is not overclocked. */
496 post_code(POST_MEM_SETUP3); // post_73h
497 banner("AUTOSIZE DIMM 0");
498 auto_size_dimm(DIMM0);
499 post_code(POST_MEM_SETUP4); // post_74h
500 banner("AUTOSIZE DIMM 1");
501 auto_size_dimm(DIMM1);
503 /* Set CAS latency */
504 banner("set cas latency");
505 post_code(POST_MEM_SETUP5); // post_75h
508 /* Set all the other latencies here (tRAS, tRP....) */
509 banner("set all latency");
512 /* Set Extended Mode Registers */
514 set_extended_mode_registers();
516 banner("set ref rate");
517 /* Set Memory Refresh Rate */
521 /* Section 6.1.3, LX processor databooks, BIOS Initialization Sequence
522 * Section 4.1.4, GX/CS5535 GeodeROM Porting guide */
523 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
528 /* 2. clock gating for PMode */
529 msr = rdmsr(MC_GLD_MSR_PM);
532 wrmsr(MC_GLD_MSR_PM, msr);
533 /* undocmented bits in GX, in LX there are
534 * 8 bits in PM1_UP_DLY */
535 msr = rdmsr(MC_CF1017_DATA);
537 wrmsr(MC_CF1017_DATA, msr);
538 //print_debug("sdram_enable step 2\n");
540 /* 3. release CKE mask to enable CKE */
541 msr = rdmsr(MC_CFCLK_DBUG);
542 msr.lo &= ~(0x03 << 8);
543 wrmsr(MC_CFCLK_DBUG, msr);
544 //print_debug("sdram_enable step 3\n");
546 /* 4. set and clear REF_TST 16 times, more shouldn't hurt
547 * why this is before EMRS and MRS ? */
548 for (i = 0; i < 19; i++) {
549 msr = rdmsr(MC_CF07_DATA);
550 msr.lo |= (0x01 << 3);
551 wrmsr(MC_CF07_DATA, msr);
552 msr.lo &= ~(0x01 << 3);
553 wrmsr(MC_CF07_DATA, msr);
555 //print_debug("sdram_enable step 4\n");
557 /* 6. enable DLL, load Extended Mode Register by set and clear PROG_DRAM */
558 msr = rdmsr(MC_CF07_DATA);
559 msr.lo |= ((0x01 << 28) | 0x01);
560 wrmsr(MC_CF07_DATA, msr);
561 msr.lo &= ~((0x01 << 28) | 0x01);
562 wrmsr(MC_CF07_DATA, msr);
563 //print_debug("sdram_enable step 6\n");
565 /* 7. Reset DLL, Bit 27 is undocumented in GX datasheet,
566 * it is documented in LX datasheet */
567 /* load Mode Register by set and clear PROG_DRAM */
568 msr = rdmsr(MC_CF07_DATA);
569 msr.lo |= ((0x01 << 27) | 0x01);
570 wrmsr(MC_CF07_DATA, msr);
571 msr.lo &= ~((0x01 << 27) | 0x01);
572 wrmsr(MC_CF07_DATA, msr);
573 //print_debug("sdram_enable step 7\n");
575 /* 8. load Mode Register by set and clear PROG_DRAM */
576 msr = rdmsr(MC_CF07_DATA);
578 wrmsr(MC_CF07_DATA, msr);
580 wrmsr(MC_CF07_DATA, msr);
581 //print_debug("sdram_enable step 8\n");
583 /* wait 200 SDCLKs */
584 for (i = 0; i < 200; i++)
588 msr = rdmsr(MC_CF_RDSYNC);
590 /* the above setting is supposed to be good for "slow" ram. We have found that for
591 * some dram, at some clock rates, e.g. hynix at 366/244, this will actually
592 * cause errors. The fix is to just set it to 0x310. Tested on 3 boards
593 * with 3 different type of dram -- Hynix, PSC, infineon.
594 * I am leaving this comment here so that at some future time nobody is tempted
595 * to mess with this setting -- RGM, 9/2006
599 wrmsr(MC_CF_RDSYNC, msr);
601 /* set delay control */
602 msr = rdmsr(GLCP_DELAY_CONTROLS);
605 wrmsr(GLCP_DELAY_CONTROLS, msr);
607 /* The RAM dll needs a write to lock on so generate a few dummy writes */
608 /* Note: The descriptor needs to be enabled to point at memory */
609 volatile unsigned long *ptr;
610 for (i = 0; i < 5; i++) {
612 *ptr = (unsigned long)i;
615 print_info("RAM DLL lock\n");