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