amdfam10: add phenom II as known cpu
[coreboot.git] / src / northbridge / amd / lx / raminit.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
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 version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <cpu/amd/lxdef.h>
21 #include <arch/io.h>
22 #include <spd.h>
23 #include "southbridge/amd/cs5536/cs5536.h"
24
25 static const unsigned char NumColAddr[] = {
26         0x00, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x07,
27         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
28 };
29
30 static void banner(const char *s)
31 {
32         printk(BIOS_DEBUG, " * %s\n", s);
33 }
34
35 static void __attribute__((noreturn)) hcf(void)
36 {
37         print_emerg("DIE\n");
38         /* this guarantees we flush the UART fifos (if any) and also
39          * ensures that things, in general, keep going so no debug output
40          * is lost
41          */
42         while (1)
43                 print_emerg_char(0);
44 }
45
46 static void auto_size_dimm(unsigned int dimm)
47 {
48         uint32_t dimm_setting;
49         uint16_t dimm_size;
50         uint8_t spd_byte;
51         msr_t msr;
52
53         dimm_setting = 0;
54
55         banner("Check present");
56         /* Check that we have a dimm */
57         if (spd_read_byte(dimm, SPD_MEMORY_TYPE) == 0xFF) {
58                 return;
59         }
60
61         banner("MODBANKS");
62         /* Field: Module Banks per DIMM */
63         /* EEPROM byte usage: (5) Number of DIMM Banks */
64         spd_byte = spd_read_byte(dimm, SPD_NUM_DIMM_BANKS);
65         if ((MIN_MOD_BANKS > spd_byte) || (spd_byte > MAX_MOD_BANKS)) {
66                 print_emerg("Number of module banks not compatible\n");
67                 post_code(ERROR_BANK_SET);
68                 hcf();
69         }
70         dimm_setting |= (spd_byte >> 1) << CF07_UPPER_D0_MB_SHIFT;
71         banner("FIELDBANKS");
72
73         /* Field: Banks per SDRAM device */
74         /* EEPROM byte usage: (17) Number of Banks on SDRAM Device */
75         spd_byte = spd_read_byte(dimm, SPD_NUM_BANKS_PER_SDRAM);
76         if ((MIN_DEV_BANKS > spd_byte) || (spd_byte > MAX_DEV_BANKS)) {
77                 print_emerg("Number of device banks not compatible\n");
78                 post_code(ERROR_BANK_SET);
79                 hcf();
80         }
81         dimm_setting |= (spd_byte >> 2) << CF07_UPPER_D0_CB_SHIFT;
82         banner("SPDNUMROWS");
83
84         /*; Field: DIMM size
85          *; EEPROM byte usage: (3)  Number of Row Addresses
86          *;                                       (4)  Number of Column Addresses
87          *;                                       (5)  Number of DIMM Banks
88          *;                                       (31) Module Bank Density
89          *; Size = Module Density * Module Banks
90          */
91         if ((spd_read_byte(dimm, SPD_NUM_ROWS) & 0xF0)
92             || (spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF0)) {
93                 print_emerg("Assymetirc DIMM not compatible\n");
94                 post_code(ERROR_UNSUPPORTED_DIMM);
95                 hcf();
96         }
97         banner("SPDBANKDENSITY");
98
99         dimm_size = spd_read_byte(dimm, SPD_BANK_DENSITY);
100         banner("DIMMSIZE");
101         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 */
102         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
104         /*       Module Density * Module Banks */
105         dimm_size <<= (dimm_setting >> CF07_UPPER_D0_MB_SHIFT) & 1;     /* shift to multiply by # DIMM banks */
106         banner("BEFORT CTZ");
107         dimm_size = __builtin_ctz(dimm_size);
108         banner("TEST DIMM SIZE>8");
109         if (dimm_size > 8) {    /* 8 is 1GB only support 1GB per DIMM */
110                 print_emerg("Only support up to 1 GB per DIMM\n");
111                 post_code(ERROR_DENSITY_DIMM);
112                 hcf();
113         }
114         dimm_setting |= dimm_size << CF07_UPPER_D0_SZ_SHIFT;
115         banner("PAGESIZE");
116
117 /*; Field: PAGE size
118 *; EEPROM byte usage: (4)  Number of Column Addresses
119 *; PageSize = 2^# Column Addresses * Data width in bytes (should be 8bytes for a normal DIMM)
120 *
121 *; But this really works by magic.
122 *;If ma[12:0] is the memory address pins, and pa[12:0] is the physical column address
123 *;that MC generates, here is how the MC assigns the pa onto the ma pins:
124 *
125 *;ma  12 11 10 09 08 07 06 05 04 03 02 01 00
126 *;-------------------------------------------
127 *;pa                                    09 08 07 06 05 04 03    (7 col addr bits = 1K page size)
128 *;pa                             10 09 08 07 06 05 04 03        (8 col addr bits = 2K page size)
129 *;pa                      11 10 09 08 07 06 05 04 03    (9 col addr bits = 4K page size)
130 *;pa               12 11 10 09 08 07 06 05 04 03        (10 col addr bits = 8K page size)
131 *;pa     13 AP 12 11 10 09 08 07 06 05 04 03    (11 col addr bits = 16K page size)
132 *;pa  14 13 AP 12 11 10 09 08 07 06 05 04 03    (12 col addr bits = 32K page size)
133 *; *AP=autoprecharge bit
134 *
135 *;Remember that pa[2:0] are zeroed out since it's a 64-bit data bus (8 bytes),
136 *;so lower 3 address bits are dont_cares.So from the table above,
137 *;it's easier to see what the old code is doing: if for example,#col_addr_bits=7(06h),
138 *;it adds 3 to get 10, then does 2^10=1K.  Get it?*/
139
140         spd_byte = NumColAddr[spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF];
141         banner("MAXCOLADDR");
142         if (spd_byte > MAX_COL_ADDR) {
143                 print_emerg("DIMM page size not compatible\n");
144                 post_code(ERROR_SET_PAGE);
145                 hcf();
146         }
147         banner(">12address test");
148         spd_byte -= 7;
149         if (spd_byte > 5) {     /* if the value is above 6 it means >12 address lines */
150                 spd_byte = 7;   /* which means >32k so set to disabled */
151         }
152         dimm_setting |= spd_byte << CF07_UPPER_D0_PSZ_SHIFT;    /* 0=1k,1=2k,2=4k,etc */
153
154         banner("RDMSR CF07");
155         msr = rdmsr(MC_CF07_DATA);
156         banner("WRMSR CF07");
157         if (dimm == DIMM0) {
158                 msr.hi &= 0xFFFF0000;
159                 msr.hi |= dimm_setting;
160         } else {
161                 msr.hi &= 0x0000FFFF;
162                 msr.hi |= dimm_setting << 16;
163         }
164         wrmsr(MC_CF07_DATA, msr);
165         banner("ALL DONE");
166 }
167
168 static void checkDDRMax(void)
169 {
170         uint8_t spd_byte0, spd_byte1;
171         uint16_t speed;
172
173         /* PC133 identifier */
174         spd_byte0 = spd_read_byte(DIMM0, SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
175         if (spd_byte0 == 0xFF) {
176                 spd_byte0 = 0;
177         }
178         spd_byte1 = spd_read_byte(DIMM1, SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
179         if (spd_byte1 == 0xFF) {
180                 spd_byte1 = 0;
181         }
182
183         /* I don't think you need this check.
184            if (spd_byte0 >= 0xA0 || spd_byte1 >= 0xA0){
185            print_emerg("DIMM overclocked. Check GeodeLink Speed\n");
186            post_code(POST_PLL_MEM_FAIL);
187            hcf();
188            } */
189
190         /* Use the slowest DIMM */
191         if (spd_byte0 < spd_byte1) {
192                 spd_byte0 = spd_byte1;
193         }
194
195         /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
196         speed = 20000 / (((spd_byte0 >> 4) * 10) + (spd_byte0 & 0x0F));
197
198         /* current speed > max speed? */
199         if (GeodeLinkSpeed() > speed) {
200                 print_emerg("DIMM overclocked. Check GeodeLink Speed\n");
201                 post_code(POST_PLL_MEM_FAIL);
202                 hcf();
203         }
204 }
205
206 const uint16_t REF_RATE[] = { 15, 3, 7, 31, 62, 125 };  /* ns */
207
208 static void set_refresh_rate(void)
209 {
210         uint8_t spd_byte0, spd_byte1;
211         uint16_t rate0, rate1;
212         msr_t msr;
213
214         spd_byte0 = spd_read_byte(DIMM0, SPD_REFRESH);
215         spd_byte0 &= 0xF;
216         if (spd_byte0 > 5) {
217                 spd_byte0 = 5;
218         }
219         rate0 = REF_RATE[spd_byte0];
220
221         spd_byte1 = spd_read_byte(DIMM1, SPD_REFRESH);
222         spd_byte1 &= 0xF;
223         if (spd_byte1 > 5) {
224                 spd_byte1 = 5;
225         }
226         rate1 = REF_RATE[spd_byte1];
227
228         /* Use the faster rate (lowest number) */
229         if (rate0 > rate1) {
230                 rate0 = rate1;
231         }
232
233         msr = rdmsr(MC_CF07_DATA);
234         msr.lo |= ((rate0 * (GeodeLinkSpeed() / 2)) / 16)
235                         << CF07_LOWER_REF_INT_SHIFT;
236         wrmsr(MC_CF07_DATA, msr);
237 }
238
239 const uint8_t CASDDR[] = { 5, 5, 2, 6, 3, 7, 4, 0 };    /* 1(1.5), 1.5, 2, 2.5, 3, 3.5, 4, 0 */
240
241 static u8 getcasmap(u32 dimm, u16 glspeed)
242 {
243         u16 dimm_speed;
244         u8 spd_byte, casmap, casmap_shift=0;
245
246         /**************************      DIMM0  **********************************/
247         casmap = spd_read_byte(dimm, SPD_ACCEPTABLE_CAS_LATENCIES);
248         if (casmap != 0xFF) {
249                 /* IF -.5 timing is supported, check -.5 timing > GeodeLink */
250                 spd_byte = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_2ND);
251                 if (spd_byte != 0) {
252                         /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
253                         dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
254                         if (dimm_speed >= glspeed) {
255                                 casmap_shift = 1; /* -.5 is a shift of 1 */
256                                 /* IF -1 timing is supported, check -1 timing > GeodeLink */
257                                 spd_byte = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_3RD);
258                                 if (spd_byte != 0) {
259                                         /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
260                                         dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
261                                         if (dimm_speed >= glspeed) {
262                                                 casmap_shift = 2; /* -1 is a shift of 2 */
263                                         }
264                                 }       /* SPD_SDRAM_CYCLE_TIME_3RD (-1) !=0 */
265                         } else {
266                                 casmap_shift = 0;
267                         }
268                 }       /* SPD_SDRAM_CYCLE_TIME_2ND (-.5) !=0 */
269                 /* set the casmap based on the shift to limit possible CAS settings */
270                 spd_byte = 31 - __builtin_clz((uint32_t) casmap);
271                 /* just want bits in the lower byte since we have to cast to a 32 */
272                 casmap &= 0xFF << (spd_byte - casmap_shift);
273         } else {                /* No DIMM */
274                 casmap = 0;
275         }
276         return casmap;
277 }
278
279 static void setCAS(void)
280 {
281 /*;*****************************************************************************
282 ;*
283 ;*      setCAS
284 ;*      EEPROM byte usage: (18) SDRAM device attributes - CAS latency
285 ;*      EEPROM byte usage: (23) SDRAM Minimum Clock Cycle Time @ CLX -.5
286 ;*      EEPROM byte usage: (25) SDRAM Minimum Clock Cycle Time @ CLX -1
287 ;*
288 ;*      The CAS setting is based on the information provided in each DIMMs SPD.
289 ;*       The speed at which a DIMM can run is described relative to the slowest
290 ;*       CAS the DIMM supports. Each speed for the relative CAS settings is
291 ;*       checked that it is within the GeodeLink speed. If it isn't within the GeodeLink
292 ;*       speed, the CAS setting  is removed from the list of good settings for
293 ;*       the DIMM. This is done for both DIMMs and the lists are compared to
294 ;*       find the lowest common CAS latency setting. If there are no CAS settings
295 ;*       in common we out a ERROR_DIFF_DIMMS (78h) to port 80h and halt.
296 ;*
297 ;*      Entry:
298 ;*      Exit: Set fastest CAS Latency based on GeodeLink speed and SPD information.
299 ;*      Destroys: We really use everything !
300 ;*****************************************************************************/
301         uint16_t glspeed;
302         uint8_t spd_byte, casmap0, casmap1;
303         msr_t msr;
304
305         glspeed = GeodeLinkSpeed();
306
307         casmap0 = getcasmap(DIMM0, glspeed);
308         casmap1 = getcasmap(DIMM1, glspeed);
309
310         /*********************  CAS_LAT MAP COMPARE     ***************************/
311         if (casmap0 == 0) {
312                 spd_byte = CASDDR[__builtin_ctz(casmap1)];
313         } else if (casmap1 == 0) {
314                 spd_byte = CASDDR[__builtin_ctz(casmap0)];
315         } else if ((casmap0 &= casmap1)) {
316                 spd_byte = CASDDR[__builtin_ctz(casmap0)];
317         } else {
318                 print_emerg("DIMM CAS Latencies not compatible\n");
319                 post_code(ERROR_DIFF_DIMMS);
320                 hcf();
321         }
322
323         msr = rdmsr(MC_CF8F_DATA);
324         msr.lo &= ~(7 << CF8F_LOWER_CAS_LAT_SHIFT);
325         msr.lo |= spd_byte << CF8F_LOWER_CAS_LAT_SHIFT;
326         wrmsr(MC_CF8F_DATA, msr);
327 }
328
329 static void set_latencies(void)
330 {
331         uint32_t memspeed, dimm_setting;
332         uint8_t spd_byte0, spd_byte1;
333         msr_t msr;
334
335         memspeed = GeodeLinkSpeed() / 2;
336         dimm_setting = 0;
337
338         /* MC_CF8F setup */
339         /* tRAS */
340         spd_byte0 = spd_read_byte(DIMM0, SPD_tRAS);
341         if (spd_byte0 == 0xFF) {
342                 spd_byte0 = 0;
343         }
344         spd_byte1 = spd_read_byte(DIMM1, SPD_tRAS);
345         if (spd_byte1 == 0xFF) {
346                 spd_byte1 = 0;
347         }
348         if (spd_byte0 < spd_byte1) {
349                 spd_byte0 = spd_byte1;
350         }
351
352         /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
353         spd_byte1 = (spd_byte0 * memspeed) / 1000;
354         if (((spd_byte0 * memspeed) % 1000)) {
355                 ++spd_byte1;
356         }
357         dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2PRE_SHIFT;
358
359         /* tRP */
360         spd_byte0 = spd_read_byte(DIMM0, SPD_tRP);
361         if (spd_byte0 == 0xFF) {
362                 spd_byte0 = 0;
363         }
364         spd_byte1 = spd_read_byte(DIMM1, SPD_tRP);
365         if (spd_byte1 == 0xFF) {
366                 spd_byte1 = 0;
367         }
368         if (spd_byte0 < spd_byte1) {
369                 spd_byte0 = spd_byte1;
370         }
371
372         /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
373         spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
374         if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
375                 ++spd_byte1;
376         }
377         dimm_setting |= spd_byte1 << CF8F_LOWER_PRE2ACT_SHIFT;
378
379         /* tRCD */
380         spd_byte0 = spd_read_byte(DIMM0, SPD_tRCD);
381         if (spd_byte0 == 0xFF) {
382                 spd_byte0 = 0;
383         }
384         spd_byte1 = spd_read_byte(DIMM1, SPD_tRCD);
385         if (spd_byte1 == 0xFF) {
386                 spd_byte1 = 0;
387         }
388         if (spd_byte0 < spd_byte1) {
389                 spd_byte0 = spd_byte1;
390         }
391
392         /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
393         spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
394         if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
395                 ++spd_byte1;
396         }
397         dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2CMD_SHIFT;
398
399         /* tRRD */
400         spd_byte0 = spd_read_byte(DIMM0, SPD_tRRD);
401         if (spd_byte0 == 0xFF) {
402                 spd_byte0 = 0;
403         }
404         spd_byte1 = spd_read_byte(DIMM1, SPD_tRRD);
405         if (spd_byte1 == 0xFF) {
406                 spd_byte1 = 0;
407         }
408         if (spd_byte0 < spd_byte1) {
409                 spd_byte0 = spd_byte1;
410         }
411
412         /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
413         spd_byte1 = ((spd_byte0 >> 2) * memspeed) / 1000;
414         if ((((spd_byte0 >> 2) * memspeed) % 1000)) {
415                 ++spd_byte1;
416         }
417         dimm_setting |= spd_byte1 << CF8F_LOWER_ACT2ACT_SHIFT;
418
419         /* tRC = tRP + tRAS */
420         dimm_setting |= (((dimm_setting >> CF8F_LOWER_ACT2PRE_SHIFT) & 0x0F) +
421                         ((dimm_setting >> CF8F_LOWER_PRE2ACT_SHIFT) & 0x07))
422                                 << CF8F_LOWER_ACT2ACTREF_SHIFT;
423
424         msr = rdmsr(MC_CF8F_DATA);
425         msr.lo &= 0xF00000FF;
426         msr.lo |= dimm_setting;
427         msr.hi |= CF8F_UPPER_REORDER_DIS_SET;
428         wrmsr(MC_CF8F_DATA, msr);
429
430         /* MC_CF1017 setup */
431         /* tRFC */
432         spd_byte0 = spd_read_byte(DIMM0, SPD_tRFC);
433         if (spd_byte0 == 0xFF) {
434                 spd_byte0 = 0;
435         }
436         spd_byte1 = spd_read_byte(DIMM1, SPD_tRFC);
437         if (spd_byte1 == 0xFF) {
438                 spd_byte1 = 0;
439         }
440         if (spd_byte0 < spd_byte1) {
441                 spd_byte0 = spd_byte1;
442         }
443
444         if (spd_byte0) {
445                 /* (ns/(1/MHz) = (us*MHZ)/1000 = clocks/1000 = clocks) */
446                 spd_byte1 = (spd_byte0 * memspeed) / 1000;
447                 if (((spd_byte0 * memspeed) % 1000)) {
448                         ++spd_byte1;
449                 }
450         } else {                /* Not all SPDs have tRFC setting. Use this formula tRFC = tRC + 1 clk */
451                 spd_byte1 = ((dimm_setting >> CF8F_LOWER_ACT2ACTREF_SHIFT) & 0x0F) + 1;
452         }
453         dimm_setting = spd_byte1 << CF1017_LOWER_REF2ACT_SHIFT; /* note this clears the cf8f dimm setting */
454         msr = rdmsr(MC_CF1017_DATA);
455         msr.lo &= ~(0x1F << CF1017_LOWER_REF2ACT_SHIFT);
456         msr.lo |= dimm_setting;
457         wrmsr(MC_CF1017_DATA, msr);
458
459         /* tWTR: Set tWTR to 2 for 400MHz and above GLBUS (200Mhz mem) other wise it stay default(1) */
460         if (memspeed > 198) {
461                 msr = rdmsr(MC_CF1017_DATA);
462                 msr.lo &= ~(0x7 << CF1017_LOWER_WR_TO_RD_SHIFT);
463                 msr.lo |= 2 << CF1017_LOWER_WR_TO_RD_SHIFT;
464                 wrmsr(MC_CF1017_DATA, msr);
465         }
466 }
467
468 static void set_extended_mode_registers(void)
469 {
470         uint8_t spd_byte0, spd_byte1;
471         msr_t msr;
472         spd_byte0 = spd_read_byte(DIMM0, SPD_DEVICE_ATTRIBUTES_GENERAL);
473         if (spd_byte0 == 0xFF) {
474                 spd_byte0 = 0;
475         }
476         spd_byte1 = spd_read_byte(DIMM1, SPD_DEVICE_ATTRIBUTES_GENERAL);
477         if (spd_byte1 == 0xFF) {
478                 spd_byte1 = 0;
479         }
480         spd_byte1 &= spd_byte0;
481
482         msr = rdmsr(MC_CF07_DATA);
483         if (spd_byte1 & 1) {    /* Drive Strength Control */
484                 msr.lo |= CF07_LOWER_EMR_DRV_SET;
485         }
486         if (spd_byte1 & 2) {    /* FET Control */
487                 msr.lo |= CF07_LOWER_EMR_QFC_SET;
488         }
489         wrmsr(MC_CF07_DATA, msr);
490 }
491
492 #undef TLA_MEMORY_DEBUG
493 #ifdef TLA_MEMORY_DEBUG
494 static void EnableMTest(void)
495 {
496         msr_t msr;
497
498         msr = rdmsr(GLCP_DELAY_CONTROLS);
499         msr.hi &= ~(7 << 20);   /* clear bits 54:52 */
500         if (GeodeLinkSpeed() < 200) {
501                 msr.hi |= 2 << 20;
502         }
503         wrmsr(GLCP_DELAY_CONTROLS, msr);
504
505         msr = rdmsr(MC_CFCLK_DBUG);
506         msr.hi |=
507             CFCLK_UPPER_MTST_B2B_DIS_SET | CFCLK_UPPER_MTEST_EN_SET |
508             CFCLK_UPPER_MTST_RBEX_EN_SET;
509         msr.lo |= CFCLK_LOWER_TRISTATE_DIS_SET;
510         wrmsr(MC_CFCLK_DBUG, msr);
511
512         print_info("Enabled MTest for TLA debug\n");
513 }
514 #endif
515
516 static void sdram_set_registers(const struct mem_controller *ctrl)
517 {
518         msr_t msr;
519         uint32_t msrnum;
520
521         /* Set Timing Control */
522         msrnum = MC_CF1017_DATA;
523         msr = rdmsr(msrnum);
524         msr.lo &= ~(7 << CF1017_LOWER_RD_TMG_CTL_SHIFT);
525         if (GeodeLinkSpeed() < 334) {
526                 msr.lo |= (3 << CF1017_LOWER_RD_TMG_CTL_SHIFT);
527         } else {
528                 msr.lo |= (4 << CF1017_LOWER_RD_TMG_CTL_SHIFT);
529         }
530         wrmsr(msrnum, msr);
531
532         /* Set Refresh Staggering */
533         msrnum = MC_CF07_DATA;
534         msr = rdmsr(msrnum);
535         msr.lo &= ~0xF0;
536         msr.lo |= 0x40;         /* set refresh to 4SDRAM clocks */
537         wrmsr(msrnum, msr);
538
539         /* Memory Interleave: Set HOI here otherwise default is LOI */
540         /* msrnum = MC_CF8F_DATA;
541            msr = rdmsr(msrnum);
542            msr.hi |= CF8F_UPPER_HOI_LOI_SET;
543            wrmsr(msrnum, msr); */
544 }
545
546 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
547 {
548         uint8_t spd_byte;
549
550         banner("sdram_set_spd_register");
551         post_code(POST_MEM_SETUP);      // post_70h
552
553         spd_byte = spd_read_byte(DIMM0, SPD_MODULE_ATTRIBUTES);
554         banner("Check DIMM 0");
555         /* Check DIMM is not Register and not Buffered DIMMs. */
556         if ((spd_byte != 0xFF) && (spd_byte & 3)) {
557                 print_emerg("DIMM0 NOT COMPATIBLE\n");
558                 post_code(ERROR_UNSUPPORTED_DIMM);
559                 hcf();
560         }
561         banner("Check DIMM 1");
562         spd_byte = spd_read_byte(DIMM1, SPD_MODULE_ATTRIBUTES);
563         if ((spd_byte != 0xFF) && (spd_byte & 3)) {
564                 print_emerg("DIMM1 NOT COMPATIBLE\n");
565                 post_code(ERROR_UNSUPPORTED_DIMM);
566                 hcf();
567         }
568
569         post_code(POST_MEM_SETUP2);     // post_72h
570         banner("Check DDR MAX");
571
572         /* Check that the memory is not overclocked. */
573         checkDDRMax();
574
575         /* Size the DIMMS */
576         post_code(POST_MEM_SETUP3);     // post_73h
577         banner("AUTOSIZE DIMM 0");
578         auto_size_dimm(DIMM0);
579         post_code(POST_MEM_SETUP4);     // post_74h
580         banner("AUTOSIZE DIMM 1");
581         auto_size_dimm(DIMM1);
582
583         /* Set CAS latency */
584         banner("set cas latency");
585         post_code(POST_MEM_SETUP5);     // post_75h
586         setCAS();
587
588         /* Set all the other latencies here (tRAS, tRP....) */
589         banner("set all latency");
590         set_latencies();
591
592         /* Set Extended Mode Registers */
593         banner("set emrs");
594         set_extended_mode_registers();
595
596         banner("set ref rate");
597         /* Set Memory Refresh Rate */
598         set_refresh_rate();
599
600 }
601
602 /* Section 6.1.3, LX processor databooks, BIOS Initialization Sequence
603  * Section 4.1.4, GX/CS5535 GeodeROM Porting guide */
604 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
605 {
606         uint32_t i, msrnum;
607         msr_t msr;
608
609 /*********************************************************************
610 ;* Turn on MC/DIMM interface per JEDEC
611 ;* 1) Clock stabilizes > 200us
612 ;* 2) Assert CKE
613 ;* 3) Precharge All to put all banks into an idles state
614 ;* 4) EMRS to enable DLL
615 ;* 6) MRS w/ memory config & reset DLL set
616 ;* 7) Wait 200 clocks (2us)
617 ;* 8) Precharge All and 2 Auto refresh
618 ;* 9) MRS w/ memory config & reset DLL clear
619 ;* 8) DDR SDRAM ready for normal operation
620 ;********************************************************************/
621         post_code(POST_MEM_ENABLE);     // post_76h
622
623 #ifdef TLA_MEMORY_DEBUG
624         /* Only enable MTest for TLA memory debug */
625         EnableMTest();
626 #endif
627
628         /* If both Page Size = "Not Installed" we have a problems and should halt. */
629         msr = rdmsr(MC_CF07_DATA);
630         if ((msr.hi & ((7 << CF07_UPPER_D1_PSZ_SHIFT) | (7 << CF07_UPPER_D0_PSZ_SHIFT))) ==
631                         ((7 << CF07_UPPER_D1_PSZ_SHIFT) | (7 << CF07_UPPER_D0_PSZ_SHIFT))) {
632                 print_emerg("No memory in the system\n");
633                 post_code(ERROR_NO_DIMMS);
634                 hcf();
635         }
636
637         /*      Set CKEs */
638         msrnum = MC_CFCLK_DBUG;
639         msr = rdmsr(msrnum);
640         msr.lo &= ~(CFCLK_LOWER_MASK_CKE_SET0 | CFCLK_LOWER_MASK_CKE_SET1);
641         wrmsr(msrnum, msr);
642
643         /* Force Precharge All on next command, EMRS */
644         msrnum = MC_CFCLK_DBUG;
645         msr = rdmsr(msrnum);
646         msr.lo |= CFCLK_LOWER_FORCE_PRE_SET;
647         wrmsr(msrnum, msr);
648
649         /* EMRS to enable DLL (pre-setup done in setExtendedModeRegisters) */
650         msrnum = MC_CF07_DATA;
651         msr = rdmsr(msrnum);
652         msr.lo |= CF07_LOWER_PROG_DRAM_SET | CF07_LOWER_LOAD_MODE_DDR_SET;
653         wrmsr(msrnum, msr);
654         msr.lo &= ~(CF07_LOWER_PROG_DRAM_SET | CF07_LOWER_LOAD_MODE_DDR_SET);
655         wrmsr(msrnum, msr);
656
657         /* Clear Force Precharge All */
658         msrnum = MC_CFCLK_DBUG;
659         msr = rdmsr(msrnum);
660         msr.lo &= ~CFCLK_LOWER_FORCE_PRE_SET;
661         wrmsr(msrnum, msr);
662
663         /* MRS Reset DLL - set */
664         msrnum = MC_CF07_DATA;
665         msr = rdmsr(msrnum);
666         msr.lo |= CF07_LOWER_PROG_DRAM_SET | CF07_LOWER_LOAD_MODE_DLL_RESET;
667         wrmsr(msrnum, msr);
668         msr.lo &= ~(CF07_LOWER_PROG_DRAM_SET | CF07_LOWER_LOAD_MODE_DLL_RESET);
669         wrmsr(msrnum, msr);
670
671         /* 2us delay (200 clocks @ 200Mhz). We probably really don't need this but.... better safe. */
672         /* Wait 2 PORT61 ticks. between 15us and 30us */
673         /* This would be endless if the timer is stuck. */
674         while ((inb(0x61))) ;   /* find the first edge */
675         while (!(~inb(0x61))) ;
676
677         /* Force Precharge All on the next command, auto-refresh */
678         msrnum = MC_CFCLK_DBUG;
679         msr = rdmsr(msrnum);
680         msr.lo |= CFCLK_LOWER_FORCE_PRE_SET;
681         wrmsr(msrnum, msr);
682
683         /* Manually AUTO refresh #1 */
684         /* If auto refresh was not enabled above we would need to do 8 refreshes to prime the pump before these 2. */
685         msrnum = MC_CF07_DATA;
686         msr = rdmsr(msrnum);
687         msr.lo |= CF07_LOWER_REF_TEST_SET;
688         wrmsr(msrnum, msr);
689         msr.lo &= ~CF07_LOWER_REF_TEST_SET;
690         wrmsr(msrnum, msr);
691
692         /* Clear Force Precharge All */
693         msrnum = MC_CFCLK_DBUG;
694         msr = rdmsr(msrnum);
695         msr.lo &= ~CFCLK_LOWER_FORCE_PRE_SET;
696         wrmsr(msrnum, msr);
697
698         /* Manually AUTO refresh */
699         /* The MC should insert the right delay between the refreshes */
700         msrnum = MC_CF07_DATA;
701         msr = rdmsr(msrnum);
702         msr.lo |= CF07_LOWER_REF_TEST_SET;
703         wrmsr(msrnum, msr);
704         msr.lo &= ~CF07_LOWER_REF_TEST_SET;
705         wrmsr(msrnum, msr);
706
707         /* MRS Reset DLL - clear */
708         msrnum = MC_CF07_DATA;
709         msr = rdmsr(msrnum);
710         msr.lo |= CF07_LOWER_PROG_DRAM_SET;
711         wrmsr(msrnum, msr);
712         msr.lo &= ~CF07_LOWER_PROG_DRAM_SET;
713         wrmsr(msrnum, msr);
714
715         /* Allow MC to tristate during idle cycles with MTEST OFF */
716         msrnum = MC_CFCLK_DBUG;
717         msr = rdmsr(msrnum);
718         msr.lo &= ~CFCLK_LOWER_TRISTATE_DIS_SET;
719         wrmsr(msrnum, msr);
720
721         /* Disable SDCLK DIMM1 slot if no DIMM installed to save power. */
722         msr = rdmsr(MC_CF07_DATA);
723         if ((msr.hi & (7 << CF07_UPPER_D1_PSZ_SHIFT)) ==
724             (7 << CF07_UPPER_D1_PSZ_SHIFT)) {
725                 msrnum = GLCP_DELAY_CONTROLS;
726                 msr = rdmsr(msrnum);
727                 msr.hi |= (1 << 23);    /* SDCLK bit for 2.0 */
728                 wrmsr(msrnum, msr);
729         }
730
731         /* Set PMode0 Sensitivity Counter */
732         msr.lo = 0;             /* pmode 0=0 most aggressive */
733         msr.hi = 0x200;         /* pmode 1=200h */
734         wrmsr(MC_CF_PMCTR, msr);
735
736         /* Set PMode1 Up delay enable */
737         msrnum = MC_CF1017_DATA;
738         msr = rdmsr(msrnum);
739         msr.lo |= (209 << 8);   /* bits[15:8] = 209 */
740         wrmsr(msrnum, msr);
741
742         banner("DRAM controller init done.\n");
743         post_code(POST_MEM_SETUP_GOOD); //0x7E
744
745         /* make sure there is nothing stale in the cache */
746         /* CAR stack is in the cache __asm__ __volatile__("wbinvd\n"); */
747
748         /* The RAM dll needs a write to lock on so generate a few dummy writes */
749         /* Note: The descriptor needs to be enabled to point at memory */
750         volatile unsigned long *ptr;
751         for (i = 0; i < 5; i++) {
752                 ptr = (void *)i;
753                 *ptr = (unsigned long)i;
754         }
755         /* SWAPSiF for PBZ 4112 (Errata 34) */
756         /* check for failed DLL settings now that we have done a memory write. */
757         msrnum = GLCP_DELAY_CONTROLS;
758         msr = rdmsr(msrnum);
759         if ((msr.lo & 0x7FF) == 0x104) {
760
761                 /* If you had it you would need to clear out the fail boot count flag */
762                 /*       (depending on where it counts from etc). */
763
764                 /* The reset we are about to perform clears the PM_SSC register in the */
765                 /*       5536 so will need to store the S3 resume flag in NVRAM otherwise */
766                 /*       it would do a normal boot */
767
768                 /* Reset the system */
769                 msrnum = MDD_SOFT_RESET;
770                 msr = rdmsr(msrnum);
771                 msr.lo |= 1;
772                 wrmsr(msrnum, msr);
773         }
774         print_info("RAM DLL lock\n");
775
776 }