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