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