eb43c6861f76bb6d06c975bae87dbd7cbfefb3f3
[coreboot.git] / src / northbridge / amd / gx2 / northbridgeinit.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <bitops.h>
10 #include "chip.h"
11 #include "northbridge.h"
12 #include <cpu/amd/gx2def.h>
13 #include <cpu/x86/msr.h>
14 #include <cpu/x86/cache.h>
15
16 /* put this here for now, we are not sure where it belongs */
17
18 struct gliutable {
19         unsigned long desc_name;
20         unsigned short desc_type;
21         unsigned long hi, lo;
22 };
23
24 struct gliutable gliu0table[] = {
25         {.desc_name=MSR_GLIU0_BASE1,  .desc_type= BM,.hi= MSR_MC + 0x0,.lo=  0x0FFF80},         /*  0-7FFFF to MC*/
26         {.desc_name=MSR_GLIU0_BASE2,  .desc_type= BM,.hi= MSR_MC + 0x0,.lo=(0x80 << 20) + 0x0FFFE0},            /*  80000-9ffff to Mc*/
27         {.desc_name=MSR_GLIU0_SHADOW, .desc_type= SC_SHADOW,.hi=  MSR_MC + 0x0,.lo=  0x03},     /*  C0000-Fffff split to MC and PCI (sub decode) A0000-Bffff handled by SoftVideo*/
28         {.desc_name=MSR_GLIU0_SYSMEM, .desc_type= R_SYSMEM,.hi=  MSR_MC,.lo=  0x0},             /*  Catch and fix dynamicly.*/
29         {.desc_name=MSR_GLIU0_DMM,    .desc_type= BMO_DMM,.hi=  MSR_MC,.lo=  0x0},              /*  Catch and fix dynamicly.*/
30         {.desc_name=MSR_GLIU0_SMM,    .desc_type= BMO_SMM,.hi=  MSR_MC,.lo=  0x0},              /*  Catch and fix dynamicly.*/
31         {.desc_name=GLIU0_GLD_MSR_COH,.desc_type= OTHER,.hi= 0x0,.lo= GL0_CPU},
32         {.desc_name=GL_END,           .desc_type= GL_END,.hi= 0x0,.lo= 0x0},
33 };
34
35
36 struct gliutable gliu1table[] = {
37         {.desc_name=MSR_GLIU1_BASE1,.desc_type=  BM,.hi=  MSR_GL0 + 0x0,.lo=  0x0FFF80},        /*  0-7FFFF to MC*/
38         {.desc_name=MSR_GLIU1_BASE2,.desc_type=  BM,.hi=  MSR_GL0 + 0x0,.lo= (0x80 << 20) +0x0FFFE0},   /*  80000-9ffff to Mc*/
39         {.desc_name=MSR_GLIU1_SHADOW,.desc_type=  SC_SHADOW,.hi=  MSR_GL0 + 0x0,.lo=  0x03}, /*  C0000-Fffff split to MC and PCI (sub decode)*/
40         {.desc_name=MSR_GLIU1_SYSMEM,.desc_type=  R_SYSMEM,.hi=  MSR_GL0,.lo=  0x0},            /*  Cat0xc and fix dynamicly.*/
41         {.desc_name=MSR_GLIU1_DMM,.desc_type=  BM_DMM,.hi=  MSR_GL0,.lo=  0x0},                 /*  Cat0xc and fix dynamicly.*/
42         {.desc_name=MSR_GLIU1_SMM,.desc_type=  BM_SMM,.hi=  MSR_GL0,.lo=  0x0},                 /*  Cat0xc and fix dynamicly.*/
43         {.desc_name=GLIU1_GLD_MSR_COH,.desc_type= OTHER,.hi= 0x0,.lo= GL1_GLIU0},
44         {.desc_name=MSR_GLIU1_FPU_TRAP,.desc_type=  SCIO,.hi=  (GL1_GLCP << 29) + 0x0,.lo=  0x033000F0},        /*  FooGlue FPU 0xF0*/
45         {.desc_name=GL_END,.desc_type= GL_END,.hi= 0x0,.lo= 0x0},
46 };
47
48 struct gliutable *gliutables[]  = {gliu0table, gliu1table, 0};
49
50 struct msrinit {
51         unsigned long msrnum;
52         msr_t msr;
53 };
54
55 struct msrinit ClockGatingDefault [] = {
56         {GLIU0_GLD_MSR_PM,      {.hi=0x00,.lo=0x0005}},
57                         /*  MC must stay off in SDR mode. It is turned on in CPUBug??? lotus #77.142*/
58         {MC_GLD_MSR_PM,         {.hi=0x00,.lo=0x0000}},
59         {GLIU1_GLD_MSR_PM,      {.hi=0x00,.lo=0x0005}},
60         {VG_GLD_MSR_PM,         {.hi=0x00,.lo=0x0000}},                 /*  lotus #77.163*/
61         {GP_GLD_MSR_PM,         {.hi=0x00,.lo=0x0001}},
62         {DF_GLD_MSR_PM,         {.hi=0x00,.lo=0x0155}},
63         {GLCP_GLD_MSR_PM,       {.hi=0x00,.lo=0x0015}},
64         {GLPCI_GLD_MSR_PM,      {.hi=0x00,.lo=0x0015}},
65         {FG_GLD_MSR_PM,         {.hi=0x00,.lo=0x0000}},                 /* Always on*/
66         {0xffffffff,                            {0xffffffff, 0xffffffff}},
67 };
68         /*  All On*/
69 struct msrinit ClockGatingAllOn[] = {
70         {GLIU0_GLD_MSR_PM,      {.hi=0x00,.lo=0x0FFFFFFFF}},
71         {MC_GLD_MSR_PM,         {.hi=0x00,.lo=0x0FFFFFFFF}},
72         {GLIU1_GLD_MSR_PM,      {.hi=0x00,.lo=0x0FFFFFFFF}},
73         {VG_GLD_MSR_PM,         {.hi=0x00, .lo=0x00}},
74         {GP_GLD_MSR_PM,         {.hi=0x00,.lo=0x000000001}},
75         {DF_GLD_MSR_PM,         {.hi=0x00,.lo=0x0FFFFFFFF}},
76         {GLCP_GLD_MSR_PM,       {.hi=0x00,.lo=0x0FFFFFFFF}},
77         {GLPCI_GLD_MSR_PM,      {.hi=0x00,.lo=0x0FFFFFFFF}},
78         {FG_GLD_MSR_PM,         {.hi=0x00,.lo=0x0000}},
79         {0xffffffff,                            {0xffffffff, 0xffffffff}},
80 };
81
82         /*  Performance*/
83 struct msrinit ClockGatingPerformance[] = {
84         {VG_GLD_MSR_PM,         {.hi=0x00,.lo=0x0000}},         /*  lotus #77.163*/
85         {GP_GLD_MSR_PM,         {.hi=0x00,.lo=0x0001}},
86         {DF_GLD_MSR_PM,         {.hi=0x00,.lo=0x0155}},
87         {GLCP_GLD_MSR_PM,       {.hi=0x00,.lo=0x0015}},
88         {0xffffffff,                            {0xffffffff, 0xffffffff}},
89 };
90 /* */
91 /*  SET GeodeLink PRIORITY*/
92 /* */
93 struct msrinit GeodeLinkPriorityTable [] = {
94         {CPU_GLD_MSR_CONFIG,            {.hi=0x00,.lo=0x0220}}, /*  CPU Priority.*/
95         {DF_GLD_MSR_MASTER_CONF,        {.hi=0x00,.lo=0x0000}}, /*  DF Priority.*/
96         {VG_GLD_MSR_CONFIG,             {.hi=0x00,.lo=0x0720}}, /*  VG Primary and Secondary Priority.*/
97         {GP_GLD_MSR_CONFIG,             {.hi=0x00,.lo=0x0010}}, /*  Graphics Priority.*/
98         {GLPCI_GLD_MSR_CONFIG,          {.hi=0x00,.lo=0x0027}}, /*  GLPCI Priority + PID*/
99         {GLCP_GLD_MSR_CONF,             {.hi=0x00,.lo=0x0001}}, /*  GLCP Priority + PID*/
100         {VIP_GLD_MSR_CONFIG,            {.hi=0x00,.lo=0x0622}}, /*  VIP PID*/
101         {AES_GLD_MSR_CONFIG,            {.hi=0x00,.lo=0x0013}}, /*  AES PID*/
102         {0x0FFFFFFFF,                   {0x0FFFFFFFF, 0x0FFFFFFFF}},    /*  END*/
103 };
104
105 /* do we have dmi or not? assume NO per AMD */
106 int havedmi = 0;
107
108 static void
109 writeglmsr(struct gliutable *gl){
110         msr_t msr;
111
112         msr.lo = gl->lo;
113         msr.hi = gl->hi;
114         wrmsr(gl->desc_name, msr);      // MSR - see table above
115         printk_debug("%s: write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
116         /* they do this, so we do this */
117         msr = rdmsr(gl->desc_name);
118         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
119 }
120
121 static void
122 ShadowInit(struct gliutable *gl)
123 {
124         msr_t msr;
125
126         msr = rdmsr(gl->desc_name);
127
128         if (msr.lo == 0) {
129                 writeglmsr(gl); 
130         }
131 }
132
133 /* NOTE: transcribed from assembly code. There is the usual redundant assembly nonsense in here. 
134   * CLEAN ME UP
135    */
136 /* yes, this duplicates later code, but it seems that is how they want it done. 
137   */
138 extern int sizeram(void);
139 static void
140 SysmemInit(struct gliutable *gl)
141 {
142         msr_t msr;
143         int sizembytes, sizebytes;
144
145         /* 
146          * Figure out how much RAM is in the machine and alocate all to the 
147          * system. We will adjust for SMM and DMM now and Frame Buffer later.
148          */
149         sizembytes = sizeram();
150         printk_debug("%s: enable for %dm bytes\n", __FUNCTION__, sizembytes);
151         sizebytes = sizembytes << 20;
152
153         sizebytes -= SMM_SIZE*1024 +1;
154
155         if (havedmi)
156                 sizebytes -= DMM_SIZE * 1024 + 1;
157
158         sizebytes -= 1;
159         msr.hi = (gl->hi & 0xFFFFFF00) | (sizebytes >> 24);
160         /* set up sizebytes to fit into msr.lo */
161         sizebytes <<= 8; /* what? well, we want bits 23:12 in bits 31:20. */
162         sizebytes &= 0xfff00000;
163         sizebytes |= 0x100;
164         msr.lo = sizebytes;
165         wrmsr(gl->desc_name, msr);      // MSR - see table above
166         msr = rdmsr(gl->desc_name);
167         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, 
168                                 gl->desc_name, msr.hi, msr.lo);
169         
170 }
171 static void
172 DMMGL0Init(struct gliutable *gl) {
173         msr_t msr;
174         int sizebytes = sizeram()<<20;
175         long offset;
176
177         if (! havedmi)
178                 return;
179
180         printk_debug("%s: %d bytes\n", __FUNCTION__, sizebytes);
181
182         sizebytes -= DMM_SIZE*1024;
183         offset = sizebytes - DMM_OFFSET;
184         printk_debug("%s: offset is 0x%08x\n", __FUNCTION__, offset);
185         offset >>= 12;
186         msr.hi = (gl->hi) | (offset << 8);
187         /* I don't think this is needed */
188         msr.hi &= 0xffffff00;
189         msr.hi |= (DMM_OFFSET >> 24);
190         msr.lo = DMM_OFFSET << 8;
191         msr.lo |= ((~(DMM_SIZE*1024)+1)>>12)&0xfffff;
192         
193         wrmsr(gl->desc_name, msr);      // MSR - See table above
194         msr = rdmsr(gl->desc_name);
195         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
196         
197 }
198 static void
199 DMMGL1Init(struct gliutable *gl) {
200         msr_t msr;
201
202         if (! havedmi)
203                 return;
204
205         printk_debug("%s:\n", __FUNCTION__ );
206
207         msr.hi = gl->hi;
208         /* I don't think this is needed */
209         msr.hi &= 0xffffff00;
210         msr.hi |= (DMM_OFFSET >> 24);
211         msr.lo = DMM_OFFSET << 8;
212         /* hmm. AMD source has SMM here ... SMM, not DMM? We think DMM */
213         printk_err("%s: warning, using DMM_SIZE even though AMD used SMM_SIZE\n", __FUNCTION__);
214         msr.lo |= ((~(DMM_SIZE*1024)+1)>>12)&0xfffff;
215         
216         wrmsr(gl->desc_name, msr);      // MSR - See table above
217         msr = rdmsr(gl->desc_name);
218         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
219 }
220 static void
221 SMMGL0Init(struct gliutable *gl) {
222         msr_t msr;
223         int sizebytes = sizeram()<<20;
224         long offset;
225
226         sizebytes -= SMM_SIZE*1024;
227
228         if (havedmi)
229                 sizebytes -= DMM_SIZE * 1024;
230
231         printk_debug("%s: %d bytes\n", __FUNCTION__, sizebytes);
232
233         offset = sizebytes - SMM_OFFSET;
234         printk_debug("%s: offset is 0x%08x\n", __FUNCTION__, offset);
235         offset >>= 12;
236
237         msr.hi = offset << 8;
238         msr.hi |= SMM_OFFSET>>24;
239
240         msr.lo = SMM_OFFSET << 8;
241         msr.lo |= ((~(SMM_SIZE*1024)+1)>>12)&0xfffff;
242         
243         wrmsr(gl->desc_name, msr);      // MSR - See table above
244         msr = rdmsr(gl->desc_name);
245         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
246 }
247 static void
248 SMMGL1Init(struct gliutable *gl) {
249         msr_t msr;
250         printk_debug("%s:\n", __FUNCTION__ );
251
252         msr.hi = gl->hi;
253         /* I don't think this is needed */
254         msr.hi &= 0xffffff00;
255         msr.hi |= (SMM_OFFSET >> 24);
256         msr.lo = SMM_OFFSET << 8;
257         msr.lo |= ((~(SMM_SIZE*1024)+1)>>12)&0xfffff;
258         
259         wrmsr(gl->desc_name, msr);      // MSR - See table above
260         msr = rdmsr(gl->desc_name);
261         printk_debug("%s: AFTER write msr 0x%08x, val 0x%08x:0x%08x\n", __FUNCTION__, gl->desc_name, msr.hi, msr.lo);
262 }
263
264 static void
265 GLIUInit(struct gliutable *gl){
266
267         while (gl->desc_type != GL_END){
268                 switch(gl->desc_type){
269                 default: 
270                         /* For Unknown types: Write then read MSR */
271                         writeglmsr(gl);
272                 case SC_SHADOW: /*  Check for a Shadow entry*/
273                         ShadowInit(gl);
274                         break;
275         
276                 case R_SYSMEM: /*  check for a SYSMEM entry*/
277                         SysmemInit(gl);
278                         break;
279         
280                 case BMO_DMM: /*  check for a DMM entry*/
281                         DMMGL0Init(gl);
282                         break;
283         
284                 case BM_DMM     : /*  check for a DMM entry*/
285                         DMMGL1Init(gl);
286                         break;
287         
288                 case BMO_SMM    : /*  check for a SMM entry*/
289                         SMMGL0Init(gl);
290                         break;
291         
292                 case BM_SMM     : /*  check for a SMM entry*/
293                         SMMGL1Init(gl); 
294                         break;
295                 }
296                 gl++;
297         }
298
299 }
300         /* ***************************************************************************/
301         /* **/
302         /* *    GLPCIInit*/
303         /* **/
304         /* *    Set up GLPCI settings for reads/write into memory*/
305         /* *    R0:  0-640KB,*/
306         /* *    R1:  1MB - Top of System Memory*/
307         /* *    R2: SMM Memory*/
308         /* *    R3: Framebuffer? - not set up yet*/
309         /* *    R4: ??*/
310         /* **/
311         /* *    Entry:*/
312         /* *    Exit:*/
313         /* *    Modified:*/
314         /* **/
315         /* ***************************************************************************/
316 static void GLPCIInit(void){
317         struct gliutable *gl = 0;
318         int i;
319         msr_t msr;
320         int msrnum;
321
322         /* */
323         /*  R0 - GLPCI settings for Conventional Memory space.*/
324         /* */
325         msr.hi =  (0x09F000 >> 12) << GLPCI_RC_UPPER_TOP_SHIFT          /*  640*/;
326         msr.lo =  0                                                     /*  0*/;
327         msr.lo |= GLPCI_RC_LOWER_EN_SET+ GLPCI_RC_LOWER_PF_SET + GLPCI_RC_LOWER_WC_SET;
328         msrnum = GLPCI_RC0;
329         wrmsr(msrnum, msr);
330
331         /* */
332         /*  R1 - GLPCI settings for SysMem space.*/
333         /* */
334         /*  Get systop from GLIU0 SYSTOP Descriptor*/
335         for(i = 0; gliu0table[i].desc_name != GL_END; i++) {
336                 if (gliu0table[i].desc_type == R_SYSMEM) {
337                         gl = &gliu0table[i];
338                         break;
339                 }
340         }
341         if (gl) {
342                 unsigned long pah, pal;
343                 msrnum = gl->desc_name;
344                 msr = rdmsr(msrnum);
345                 /* example R_SYSMEM value: 20:00:00:0f:fb:f0:01:00
346                  * translates to a base of 0x00100000 and top of 0xffbf0000
347                  * base of 1M and top of around 256M
348                  */
349                 /* we have to create a page-aligned (4KB page) address for base and top */
350                 /* So we need a high page aligned addresss (pah) and low page aligned address (pal)
351                  * pah is from msr.hi << 12 | msr.low >> 20. pal is msr.lo << 12
352                  */
353                 printk_debug("GLPCI r1: system msr.lo 0x%08x msr.hi 0x%08x\n", msr.lo, msr.hi);
354                 pah = ((msr.hi &0xff) << 12) | ((msr.lo >> 20) & 0xfff);
355                 /* we have the page address. Now make it a page-aligned address */
356                 pah <<= 12;
357
358                 pal = msr.lo << 12;
359                 msr.hi =  pah;
360                 msr.lo =  pal;
361                 msr.lo |= GLPCI_RC_LOWER_EN_SET | GLPCI_RC_LOWER_PF_SET | GLPCI_RC_LOWER_WC_SET;
362                 printk_debug("GLPCI r1: system msr.lo 0x%08x msr.hi 0x%08x\n", msr.lo, msr.hi);
363                 msrnum = GLPCI_RC1;
364                 wrmsr(msrnum, msr);
365         }
366
367         /* */
368         /*  R2 - GLPCI settings for SMM space.*/
369         /* */
370         msr.hi =  ((SMM_OFFSET+(SMM_SIZE*1024-1)) >> 12) << GLPCI_RC_UPPER_TOP_SHIFT;
371         msr.lo =  (SMM_OFFSET >> 12) << GLPCI_RC_LOWER_BASE_SHIFT;
372         msr.lo |= GLPCI_RC_LOWER_EN_SET | GLPCI_RC_LOWER_PF_SET;
373         msrnum = GLPCI_RC2;
374         wrmsr(msrnum, msr);
375
376         /* this is done elsewhere already, but it does no harm to do it more than once */
377         /*  write serialize memory hole to PCI. Need to to unWS when something is shadowed regardless of cachablility.*/
378         msr.lo =  0x021212121                                                           /*  cache disabled and write serialized*/;
379         msr.hi =  0x021212121                                                           /*  cache disabled and write serialized*/;
380
381         msrnum = CPU_RCONF_A0_BF;
382         wrmsr(msrnum, msr);
383
384         msrnum = CPU_RCONF_C0_DF;
385         wrmsr(msrnum, msr);
386
387         msrnum = CPU_RCONF_E0_FF;
388         wrmsr(msrnum, msr);
389
390         /*  Set Non-Cacheable Read Only for NorthBound Transactions to Memory. The Enable bit is handled in the Shadow setup.*/
391         msrnum = GLPCI_A0_BF;
392         msr.hi =  0x35353535;
393         msr.lo =  0x35353535;
394         wrmsr(msrnum, msr);
395
396         msrnum = GLPCI_C0_DF;
397         msr.hi =  0x35353535;
398         msr.lo =  0x35353535;
399         wrmsr(msrnum, msr);
400
401         msrnum = GLPCI_E0_FF;
402         msr.hi =  0x35353535;
403         msr.lo =  0x35353535;
404         wrmsr(msrnum, msr);
405
406         /*  Set WSREQ*/
407         msrnum = CPU_DM_CONFIG0;
408         msr = rdmsr(msrnum);
409         msr.hi &= ~ (7 << DM_CONFIG0_UPPER_WSREQ_SHIFT);
410         msr.hi |= 2 << DM_CONFIG0_UPPER_WSREQ_SHIFT     ;       /*  reduce to 1 for safe mode.*/
411         wrmsr(msrnum, msr);
412
413         /* we are ignoring the 5530 case for now, and perhaps forever. */
414
415         /* */
416         /* 5535 NB Init*/
417         /* */   
418         msrnum = GLPCI_ARB;
419         msr = rdmsr(msrnum);
420         msr.hi |=  GLPCI_ARB_UPPER_PRE0_SET | GLPCI_ARB_UPPER_PRE1_SET;
421         msr.lo |=  GLPCI_ARB_LOWER_IIE_SET;
422         wrmsr(msrnum, msr);
423
424
425         msrnum = GLPCI_CTRL;
426         msr = rdmsr(msrnum);
427
428         msr.lo |=  GLPCI_CTRL_LOWER_ME_SET | GLPCI_CTRL_LOWER_OWC_SET | GLPCI_CTRL_LOWER_PCD_SET;       /*   (Out will be disabled in CPUBUG649 for < 2.0 parts .)*/
429         msr.lo |=  GLPCI_CTRL_LOWER_LDE_SET;
430
431         msr.lo &=  ~ (0x03 << GLPCI_CTRL_LOWER_IRFC_SHIFT);
432         msr.lo |=  0x02 << GLPCI_CTRL_LOWER_IRFC_SHIFT;
433
434         msr.lo &=  ~ (0x07 << GLPCI_CTRL_LOWER_IRFT_SHIFT);
435         msr.lo |=  0x06 << GLPCI_CTRL_LOWER_IRFT_SHIFT;
436         
437         msr.hi &=  ~ (0x0f << GLPCI_CTRL_UPPER_FTH_SHIFT);
438         msr.hi |=  0x0F << GLPCI_CTRL_UPPER_FTH_SHIFT;
439         
440         msr.hi &=  ~ (0x0f << GLPCI_CTRL_UPPER_RTH_SHIFT);
441         msr.hi |=  0x0F << GLPCI_CTRL_UPPER_RTH_SHIFT;
442         
443         msr.hi &=  ~ (0x0f << GLPCI_CTRL_UPPER_SBRTH_SHIFT);
444         msr.hi |=  0x0F << GLPCI_CTRL_UPPER_SBRTH_SHIFT;
445         
446         msr.hi &=  ~ (0x03 << GLPCI_CTRL_UPPER_WTO_SHIFT);
447         msr.hi |=  0x06 << GLPCI_CTRL_UPPER_WTO_SHIFT;
448         
449         msr.hi &=  ~ (0x03 << GLPCI_CTRL_UPPER_ILTO_SHIFT);
450         msr.hi |=  0x00 << GLPCI_CTRL_UPPER_ILTO_SHIFT;
451         wrmsr(msrnum, msr);
452
453
454         /*  Set GLPCI Latency Timer.*/
455         msrnum = GLPCI_CTRL;
456         msr = rdmsr(msrnum);
457         msr.hi |=  0x1F << GLPCI_CTRL_UPPER_LAT_SHIFT;  /*  Change once 1.x is gone.*/
458         wrmsr(msrnum, msr);
459
460         /*  GLPCI_SPARE*/
461         msrnum = GLPCI_SPARE;
462         msr = rdmsr(msrnum);
463         msr.lo &=  ~ 0x7;
464         msr.lo |=  GLPCI_SPARE_LOWER_AILTO_SET | GLPCI_SPARE_LOWER_PPD_SET | GLPCI_SPARE_LOWER_PPC_SET | GLPCI_SPARE_LOWER_MPC_SET | GLPCI_SPARE_LOWER_NSE_SET | GLPCI_SPARE_LOWER_SUPO_SET;
465         wrmsr(msrnum, msr);
466
467 }
468
469
470
471         /* ***************************************************************************/
472         /* **/
473         /* *    ClockGatingInit*/
474         /* **/
475         /* *    Enable Clock Gating.*/
476         /* **/
477         /* *    Entry:*/
478         /* *    Exit:*/
479         /* *    Modified:*/
480         /* **/
481         /* ***************************************************************************/
482 static void 
483 ClockGatingInit (void){
484         msr_t msr;
485         struct msrinit *gating = ClockGatingDefault;
486         int i;
487
488 #if 0
489         mov     cx, TOKEN_CLK_GATE
490         NOSTACK bx, GetNVRAMValueBX
491         cmp     al, TVALUE_CG_OFF
492         je      gatingdone
493         
494         cmp     al, TVALUE_CG_DEFAULT
495         jb      allon
496         ja      performance
497         lea     si, ClockGatingDefault
498         jmp     nextdevice
499
500 allon:
501         lea     si, ClockGatingAllOn
502         jmp     nextdevice
503
504 performance:
505         lea     si, ClockGatingPerformance
506 #endif
507
508         for(i = 0; gating->msrnum != 0xffffffff; i++) {
509                 msr = rdmsr(gating->msrnum);
510                 printk_debug("%s: MSR 0x%08x is 0x%08x:0x%08x\n", __FUNCTION__, gating->msrnum, msr.hi, msr.lo);
511                 msr.hi |= gating->msr.hi;
512                 msr.lo |= gating->msr.lo;
513                 printk_debug("%s: MSR 0x%08x will be set to  0x%08x:0x%08x\n", __FUNCTION__, 
514                         gating->msrnum, msr.hi, msr.lo);
515                 wrmsr(gating->msrnum, msr);     // MSR - See the table above
516                 gating +=1;
517         }
518
519 }
520
521 static void 
522 GeodeLinkPriority(void){
523         msr_t msr;
524         struct msrinit *prio = GeodeLinkPriorityTable;
525         int i;
526
527         for(i = 0; prio->msrnum != 0xffffffff; i++) {
528                 msr = rdmsr(prio->msrnum);
529                 printk_debug("%s: MSR 0x%08x is 0x%08x:0x%08x\n", __FUNCTION__, prio->msrnum, msr.hi, msr.lo);
530                 msr.hi |= prio->msr.hi;
531                 msr.lo &= ~0xfff;
532                 msr.lo |= prio->msr.lo;
533                 printk_debug("%s: MSR 0x%08x will be set to 0x%08x:0x%08x\n", __FUNCTION__, 
534                         prio->msrnum, msr.hi, msr.lo);
535                 wrmsr(prio->msrnum, msr);       // MSR - See the table above
536                 prio +=1;
537         }
538 }
539
540
541         
542 /*
543  *      Get the GLIU0 shadow register settings
544  *      If the setShadow function is used then all shadow descriptors
545  *        will stay sync'ed.
546  */
547 static uint64_t getShadow(void)
548 {
549         msr_t msr;
550         msr = rdmsr(MSR_GLIU0_SHADOW);
551         return ( ( (uint64_t) msr.hi ) << 32 ) | msr.lo;
552 }
553
554
555 /*
556  *      Set the cache RConf registers for the memory hole.
557  *      Keeps all cache shadow descriptors sync'ed.
558  *      This is part of the PCI lockup solution
559  *      Entry: EDX:EAX is the shadow settings
560  */
561 static void setShadowRCONF(uint32_t shadowHi, uint32_t shadowLo)
562 {
563         // ok this is whacky bit translation time.
564         int bit;
565         uint8_t shadowByte;
566         msr_t msr;
567         shadowByte = (uint8_t) (shadowLo >> 16);
568
569         // load up D000 settings in edx.
570         for (bit = 8; (bit > 4); bit--) {
571                 msr.hi <<= 8;
572                 msr.hi |= 1;                                            // cache disable PCI/Shadow memory
573                 if (shadowByte && (1 << bit))
574                         msr.hi |= 0x20;                                 // write serialize PCI memory
575         }
576
577         // load up C000 settings in eax.
578         for ( ; bit; bit--) {
579                 msr.lo <<= 8;
580                 msr.lo |= 1;                                            // cache disable PCI/Shadow memory
581                 if (shadowByte && (1 << bit))
582                         msr.lo |= 0x20;                                 // write serialize PCI memory
583         }
584
585         wrmsr(CPU_RCONF_C0_DF, msr);
586
587         shadowByte = (uint8_t) (shadowLo >> 24);
588
589         // load up F000 settings in edx.
590         for (bit = 8; (bit > 4); bit--) {
591                 msr.hi <<= 8;
592                 msr.hi |= 1;                                            // cache disable PCI/Shadow memory
593                 if (shadowByte && (1 << bit))
594                         msr.hi |= 0x20;                                 // write serialize PCI memory
595         }
596
597         // load up E000 settings in eax.
598         for ( ; bit; bit--) {
599                 msr.lo <<= 8;
600                 msr.lo |= 1;                                            // cache disable PCI/Shadow memory
601                 if (shadowByte && (1 << bit))
602                         msr.lo |= 0x20;                                 // write serialize PCI memory
603         }
604
605         wrmsr(CPU_RCONF_E0_FF, msr);
606 }
607
608
609 /*
610  *      Set the GLPCI registers for the memory hole.
611  *      Keeps all cache shadow descriptors sync'ed.
612  *      Entry: EDX:EAX is the shadow settings
613  */
614 static void setShadowGLPCI(uint32_t shadowHi, uint32_t shadowLo)
615 {
616         msr_t msr;
617         
618 // Set the Enable Register.
619
620         msr = rdmsr(GLPCI_REN);
621         msr.lo &= 0xFFFF00FF;
622         msr.lo |= ( (shadowLo & 0xFFFF0000) >> 8);
623         wrmsr(GLPCI_REN, msr);
624 }
625
626
627 /*
628  *      Set the GLIU SC register settings. Scans descriptor tables for SC_SHADOW.
629  *      Keeps all shadow descriptors sync'ed.
630  *      Entry: EDX:EAX is the shadow settings
631  */
632 static void setShadow(uint64_t shadowSettings)
633 {
634         int i;
635         msr_t msr;
636         struct gliutable* pTable;
637         uint32_t shadowLo, shadowHi;
638
639         shadowLo = (uint32_t) shadowSettings;
640         shadowHi = (uint32_t) (shadowSettings >> 32);
641
642         setShadowRCONF(shadowHi, shadowLo);
643         setShadowGLPCI(shadowHi, shadowLo);
644
645         for(i = 0; gliutables[i]; i++) {
646                 for (pTable = gliutables[i]; pTable->desc_type != GL_END; pTable++) {
647                         if (pTable->desc_type == SC_SHADOW) {
648
649                                 msr = rdmsr(pTable->desc_name);
650                                 msr.lo = (uint32_t) shadowSettings;
651                                 msr.hi &= 0xFFFF0000;           // maintain PDID in upper EDX
652                                 msr.hi |= ((uint32_t) (shadowSettings >> 32)) & 0x0000FFFF;
653                                 wrmsr(pTable->desc_name, msr);  // MSR - See the table above
654
655                         }
656                 }
657         }
658 }
659
660 /**************************************************************************
661  *
662  *      shadowRom
663  *
664  *      Set up a stack for ease of further testing
665  *
666  *      Entry:
667  *      Exit:
668  *      Destroys:
669  *
670  **************************************************************************/
671 static void 
672 shadowRom(void)
673 {
674         uint64_t shadowSettings = getShadow();
675         shadowSettings &= (uint64_t) 0xFFFF00000000FFFFULL;     // Disable read & writes
676         shadowSettings |= (uint64_t) 0x00000000F0000000ULL;     // Enable reads for F0000-FFFFF
677         setShadow(shadowSettings);
678 }
679
680
681
682 /***************************************************************************
683  *
684  * RCONFInit
685  *        Set up RCONF_DEFAULT and any other RCONF registers needed
686  *
687  *  DEVRC_RCONF_DEFAULT:
688  *  ROMRC(63:56) = 04h   ; write protect ROMBASE
689  *  ROMBASE(36:55) = 0FFFC0h ; Top of PCI/bottom of rom chipselect area
690  *  DEVRC(35:28) =  39h  ; cache disabled in PCI memory + WS bit on + Write Combine + write burst.
691  *  SYSTOP(27:8) = top of system memory
692  *  SYSRC(7:0) = 00h             ; writeback, can set to 08h to make writethrough 
693  *
694  ***************************************************************************/
695 #define SYSMEM_RCONF_WRITETHROUGH 8
696 #define DEVRC_RCONF_DEFAULT 0x21
697 #define ROMBASE_RCONF_DEFAULT 0xFFFC0000
698 #define ROMRC_RCONF_DEFAULT 0x25
699
700 static void
701 RCONFInit(void)
702 {
703         struct gliutable *gl = 0;
704         int i;
705         msr_t msr;
706         uint8_t SysMemCacheProp;
707         uint8_t RegionProp;
708
709         /* Locate SYSMEM entry in GLIU0table */
710         for(i = 0; gliu0table[i].desc_name != GL_END; i++) {
711                 if (gliu0table[i].desc_type == R_SYSMEM) {
712                         gl = &gliu0table[i];
713                         break;
714                 }
715         }
716         if (gl == 0) {
717                 post_code(0xCE);                /* POST_RCONFInitError */
718                 while (1);
719         }
720
721 // sysdescfound:        
722         /* found the descriptor... get its contents */
723         msr = rdmsr(gl->desc_name);
724
725         /* 20 bit address -  The bottom 12 bits go into bits 20-31 in eax, the 
726          * top 8 bits go into 0-7 of edx. 
727          */
728         msr.lo = (msr.lo & 0xFFFFFF00) | (msr.hi & 0xFF);
729         msr.lo = ((msr.lo << 12) | (msr.lo >> 20)) & 0x000FFFFF;
730         msr.lo <<= RCONF_DEFAULT_LOWER_SYSTOP_SHIFT;    // 8
731         
732         // Set Default SYSMEM region properties
733         msr.lo &= ~SYSMEM_RCONF_WRITETHROUGH;   // 8 (or ~8)
734
735         // Set PCI space cache properties
736         msr.hi = (DEVRC_RCONF_DEFAULT >> 4);    // only need the bottom bits and lets clean the rest of edx
737         msr.lo |= (DEVRC_RCONF_DEFAULT << 28);
738
739         // Set the ROMBASE. This is usually FFFC0000h
740         msr.hi |= (ROMBASE_RCONF_DEFAULT >> 12) << RCONF_DEFAULT_UPPER_ROMBASE_SHIFT;
741
742         // Set ROMBASE cache properties.
743         msr.hi |= ((ROMRC_RCONF_DEFAULT >> 8) | (ROMRC_RCONF_DEFAULT << 24));
744         
745         // now program RCONF_DEFAULT
746         wrmsr(CPU_RCONF_DEFAULT, msr);
747
748         // RCONF_BYPASS: Cache tablewalk properties and SMM/DMM header access properties.
749         // Set to match system memory cache properties.
750         msr = rdmsr(CPU_RCONF_DEFAULT);
751         SysMemCacheProp = (uint8_t) (msr.lo & 0xFF);
752         msr = rdmsr(CPU_RCONF_BYPASS);
753         msr.lo = (msr.lo & 0xFFFF0000) | (SysMemCacheProp << 8) | SysMemCacheProp;
754         wrmsr(CPU_RCONF_BYPASS, msr);
755 }
756
757
758 /* ***************************************************************************/
759 /* **/
760 /* *    northBridgeInit*/
761 /* **/
762 /* *    Core Logic initialization:  Host bridge*/
763 /* **/
764 /* *    Entry:*/
765 /* *    Exit:*/
766 /* *    Modified:*/
767 /* **/
768 /* ***************************************************************************/
769
770 void
771 northbridgeinit(void)
772 {
773         msr_t msr;
774         int i;
775         printk_debug("Enter %s\n", __FUNCTION__);
776
777         for(i = 0; gliutables[i]; i++)
778                 GLIUInit(gliutables[i]);
779
780         GeodeLinkPriority();
781         
782         shadowRom();
783         
784         // GeodeROM ensures that the BIOS waits the required 1 second before 
785         // allowing anything to access PCI
786         // PCIDelay();
787         
788         RCONFInit();
789         
790         // The cacheInit function in GeodeROM tests cache and, among other things,
791         // makes sure all INVD instructions are treated as WBINVD.  We do this
792         // because we've found some programs which require this behavior.
793         // That subset of cacheInit() is implemented here:
794         msr = rdmsr(CPU_DM_CONFIG0);
795         msr.lo |= DM_CONFIG0_LOWER_WBINVD_SET;
796         wrmsr(CPU_DM_CONFIG0, msr);
797         
798         /*  Now that the descriptor to memory is set up.*/
799         /*  The memory controller needs one read to synch its lines before it can be used.*/
800         i = *(int *) 0;
801
802         GLPCIInit();
803         ClockGatingInit();
804         __asm__("FINIT\n");
805         /* CPUBugsFix -- called elsewhere */
806         printk_debug("Exit %s\n", __FUNCTION__);
807 }
808