- Add new cvs code to cvs
[coreboot.git] / src / cpu / p6 / earlymtrr.c
1 #include <cpu/p6/mtrr.h>
2 #include <cpu/p6/msr.h>
3
4 static inline unsigned long read_cr0(void)
5 {
6     unsigned long cr0;
7     asm volatile ("movl %%cr0, %0" : "=r" (cr0));
8     return cr0;
9 }
10
11 static inline void write_cr0(unsigned long cr0)
12 {
13     asm volatile ("movl %0, %%cr0" : : "r" (cr0));
14 }
15
16 /* the fixed and variable MTTRs are power-up with random values,
17  * clear them to MTRR_TYPE_UNCACHEABLE for safty.
18  */
19
20 static void early_mtrr_init(void)
21 {
22         static const unsigned long mtrr_msrs[] = {
23                 /* fixed mtrr */
24                 0x250, 0x258, 0x259,
25                 0x268, 0x269, 0x26A,
26                 0x26B, 0x26C, 0x26D,
27                 0x26E, 0x26F,
28                 /* var mtrr */
29                 0x200, 0x201, 0x202, 0x203,
30                 0x204, 0x205, 0x206, 0x207,
31                 0x208, 0x209, 0x20A, 0x20B,
32                 0x20C, 0x20D, 0x20E, 0x20F,
33                 /* NULL end of table */
34                 0
35         };
36         msr_t msr;
37         const unsigned long *msr_addr;
38         unsigned long cr0;
39
40         print_debug("Disabling cache\r\n");
41         /* Just to be sure, take all the steps to disable the cache.
42          * This may not be needed, but C3's may...
43          * Invalidate the cache */
44         asm volatile ("invd");
45
46         /* Disable the cache */
47         cr0 = read_cr0();
48         cr0 |= 0x40000000;
49         write_cr0(cr0);
50
51         /* Disable Variable MTRRs */
52         msr.hi = 0x00000000;
53         msr.lo = 0x00000000;
54         wrmsr(MTRRdefType_MSR, msr);
55
56         /* Invalidate the cache again */
57         asm volatile ("invd");
58
59         print_debug("Clearing mtrr\r\n");
60
61         /* Inialize all of the relevant msrs to 0 */
62         msr.lo = 0;
63         msr.hi = 0;
64         for(msr_addr = mtrr_msrs; *msr_addr; msr_addr++) {
65                 wrmsr(*msr_addr, msr);
66         }
67
68         /* Enable caching for 0 - 1MB using variable mtrr */
69         msr = rdmsr(0x200);
70         msr.hi &= 0xfffffff0;
71         msr.hi |= 0x00000000;
72         msr.lo &= 0x00000f00;
73         msr.lo |= 0x00000000 | MTRR_TYPE_WRBACK;
74         wrmsr(0x200, msr);
75
76         msr = rdmsr(0x201);
77         msr.hi &= 0xfffffff0;
78         msr.hi |= 0x0000000f;
79         msr.lo &= 0x000007ff;
80         msr.lo |= (~((CONFIG_LB_MEM_TOPK << 10) - 1)) | 0x800;
81         wrmsr(0x201, msr);
82
83 #if defined(XIP_ROM_SIZE) && defined(XIP_ROM_BASE)
84         print_debug("Setting XIP\r\n");
85         /* enable write through caching so we can do execute in place
86          * on the flash rom.
87          */
88         msr.hi = 0x00000000;
89         msr.lo = XIP_ROM_BASE | MTRR_TYPE_WRTHROUGH;
90         wrmsr(0x202, msr);
91         msr.hi = 0x0000000f;
92         msr.lo = ~(XIP_ROM_SIZE - 1) | 0x800;
93         wrmsr(0x203, msr);
94 #endif
95
96         /* Set the default memory type and enable fixed and variable MTRRs 
97          */
98         /* Enable Variable MTRRs */
99         msr.hi = 0x00000000;
100         msr.lo = 0x00000800;
101         wrmsr(MTRRdefType_MSR, msr);
102
103         /* Enable the cache */
104         cr0 = read_cr0();
105         cr0 &= 0x9fffffff;
106         write_cr0(cr0);
107         print_debug("Enabled the cache\r\n");
108 }