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