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