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