Revision: linuxbios@linuxbios.org--devel/freebios--devel--2.0--patch-51
[coreboot.git] / src / cpu / amd / car / cache_as_ram.inc
1 /* by yhlu 6.2005 */
2 /* We will use 4K bytes only */
3 #define CacheSize DCACHE_RAM_SIZE
4 #define CacheBase (0xd0000 - CacheSize)
5
6 #include <cpu/x86/mtrr.h>
7 #include <cpu/amd/mtrr.h>
8
9         /* Save the BIST result */
10         movl    %eax, %ebp
11
12 CacheAsRam:
13         /* hope we can skip the double set for normal part */
14 #if USE_FALLBACK_IMAGE == 1
15         
16         /* Set MtrrFixDramModEn for clear fixed mtrr */
17         xorl    %eax, %eax                      # clear %eax and %edx
18         xorl    %edx, %edx                      
19
20 enable_fixed_mtrr_dram_modify:
21         movl    $SYSCFG_MSR, %ecx
22         rdmsr
23         andl    $(~(SYSCFG_MSR_MtrrFixDramEn|SYSCFG_MSR_MtrrVarDramEn)), %eax
24         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
25         wrmsr
26
27         /* Set the default memory type and enable fixed and variable MTRRs */
28         movl    $MTRRdefType_MSR, %ecx
29         xorl    %edx, %edx
30         /* Enable Variable and Fixed MTRRs */
31         movl    $0x00000c00, %eax
32         wrmsr
33
34         /*Clear all MTRRs */
35
36         xorl    %edx, %edx
37         movl    $fixed_mtrr_msr, %esi
38 clear_fixed_var_mtrr:
39         lodsl   (%esi), %eax
40         testl   %eax, %eax
41         jz      clear_fixed_var_mtrr_out
42
43         movl    %eax, %ecx
44         xorl    %eax, %eax
45         wrmsr   
46
47         jmp     clear_fixed_var_mtrr
48 clear_fixed_var_mtrr_out:
49
50         /* Enable the MTRRs and IORRs in SYSCFG */
51         movl    $SYSCFG_MSR, %ecx
52         rdmsr
53         orl     $(SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn), %eax
54         wrmsr
55
56 #if 1
57 #if CacheSize == 0x10000 
58         /* enable caching for 64K using fixed mtrr */
59         movl    $0x268, %ecx  /* fix4k_c0000*/
60         movl    $0x06060606, %eax /* WB IO type */
61         movl    %eax, %edx
62         wrmsr
63         movl    $0x269, %ecx
64         wrmsr
65 #endif
66
67 #if CacheSize == 0x8000
68         /* enable caching for 32K using fixed mtrr */
69         movl    $0x269, %ecx  /* fix4k_c8000*/
70         movl    $0x06060606, %eax /* WB IO type */
71         movl    %eax, %edx
72         wrmsr
73 #endif
74
75         /* enable caching for 16K/8K/4K using fixed mtrr */
76         movl    $0x269, %ecx  /* fix4k_cc000*/
77 #if CacheSize == 0x4000
78         movl    $0x06060606, %edx /* WB IO type */
79 #endif
80 #if CacheSize == 0x2000
81         movl    $0x06060000, %edx /* WB IO type */
82 #endif
83 #if CacheSize == 0x1000
84         movl    $0x06000000, %edx /* WB IO type */
85 #endif
86         xorl    %eax, %eax
87         wrmsr
88
89 #else
90         /* enable caching for 64K using variable mtrr */
91         movl    $0x200, %ecx
92         xorl    %edx, %edx
93         movl     $(CacheBase | MTRR_TYPE_WRBACK), %eax
94         wrmsr
95
96         movl    $0x201, %ecx
97         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
98         movl    $((~((CacheBase + CacheSize) - 1)) | 0x800), %eax
99         wrmsr  
100         
101         /* make it to be IO by clearing RD Dram and WR Dram */
102         movl    $IORR0_BASE, %ecx
103         xorl    %edx, %edx
104         movl    $CacheBase, %eax             /* bit 3, and bit 4 = 0 mean clear RD ram and WR ram */
105         wrmsr
106
107         movl    $IORR0_MASK, %ecx
108         movl    $0x000000ff, %edx
109         movl    $(~((CacheBase + CacheSize) - 1) | 0x800), %eax
110         wrmsr 
111 #endif
112
113         /* enable memory access for 0 - 1MB using top_mem */
114         movl    $TOP_MEM, %ecx
115         xorl    %edx, %edx
116         movl    $(((CONFIG_LB_MEM_TOPK << 10) + TOP_MEM_MASK) & ~TOP_MEM_MASK) , %eax
117         wrmsr
118 #else
119         /* disable cache */
120         movl    %cr0, %eax
121         orl    $(0x1<<30),%eax
122         movl    %eax, %cr0
123
124 #endif /*  USE_FALLBACK_IMAGE == 1*/
125
126 #if defined(XIP_ROM_SIZE) && defined(XIP_ROM_BASE)
127         /* enable write base caching so we can do execute in place
128          * on the flash rom.
129          */
130         movl    $0x202, %ecx
131         xorl    %edx, %edx
132         movl    $(XIP_ROM_BASE | MTRR_TYPE_WRBACK), %eax
133         wrmsr
134
135         movl    $0x203, %ecx
136         movl    $0x0000000f, %edx
137         movl    $(~(XIP_ROM_SIZE - 1) | 0x800), %eax
138         wrmsr
139 #endif /* XIP_ROM_SIZE && XIP_ROM_BASE */
140
141         /* enable cache */
142         movl    %cr0, %eax
143         andl    $0x9fffffff,%eax
144         movl    %eax, %cr0
145
146 #if USE_FALLBACK_IMAGE == 1
147
148
149         /* Read the range with lodsl*/
150         movl    $(CacheBase+CacheSize-4), %esi
151         std
152         movl    $(CacheSize>>2), %ecx
153         rep     lodsl
154         /* Clear the range */
155         movl    $(CacheBase+CacheSize-4), %edi
156         movl    $(CacheSize>>2), %ecx
157         xorl    %eax, %eax
158         rep     stosl
159
160 #if 0
161         /* check the cache as ram */
162         movl  $CacheBase, %esi
163         movl    $(CacheSize>>2), %ecx
164 .xin1:
165         movl  %esi, %eax
166         movl  %eax, (%esi)
167         movl    $0x1000, %edx
168         movb    %ah, %al
169 .testx1:  
170         outb %al, $0x80
171         decl    %edx
172         jnz .testx1
173
174         movl  (%esi), %eax
175         cmpb 0xff, %al
176         je .xin2  /* dont show */
177         movl    $0x1000, %edx
178 .testx2:
179         outb %al, $0x80
180         decl    %edx
181         jnz .testx2
182
183 .xin2:  decl     %ecx
184         je      .xout1
185         add     $4, %esi
186         jmp     .xin1
187 .xout1:
188
189 #endif
190 #endif /*USE_FALLBACK_IMAGE == 1*/
191
192
193         movl    $(CacheBase+CacheSize-4), %eax
194         movl    %eax, %esp
195
196
197         /* Restore the BIST result */
198         movl    %ebp, %eax
199         /* We need to set ebp ? No need */
200         movl    %esp, %ebp
201         pushl %eax  /* bist */
202         call    amd64_main
203         /* We will not go back */
204
205 fixed_mtrr_msr: 
206         .long   0x250, 0x258, 0x259
207         .long   0x268, 0x269, 0x26A
208         .long   0x26B, 0x26C, 0x26D
209         .long   0x26E, 0x26F
210 var_mtrr_msr:   
211         .long   0x200, 0x201, 0x202, 0x203
212         .long   0x204, 0x205, 0x206, 0x207
213         .long   0x208, 0x209, 0x20A, 0x20B
214         .long   0x20C, 0x20D, 0x20E, 0x20F
215 var_iorr_msr:   
216         .long   0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019
217 mem_top:
218         .long   0xC001001A, 0xC001001D
219         .long   0x000 /* NULL, end of table */
220 .CacheAsRam_out: