f46e5bdc480c9e15dff48794234b1fb84d893ddb
[coreboot.git] / src / cpu / intel / model_6fx / cache_as_ram.inc
1 /* 
2  * This file is part of the coreboot project.
3  * 
4  * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5  * Copyright (C) 2007-2008 coresystems GmbH
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
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 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
22 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
23
24 #include <cpu/x86/stack.h>
25 #include <cpu/x86/mtrr.h>
26 #include <cpu/amd/mtrr.h>
27
28         /* Save the BIST result */
29         movl    %eax, %ebp
30
31 cache_as_ram:
32         post_code(0x20)
33
34         /* Send INIT IPI to all excluding ourself */
35         movl    $0x000C4500, %eax
36         movl    $0xFEE00300, %esi
37         movl    %eax, (%esi)
38
39         /* Disable prefetchers */
40         movl    $0x01a0, %ecx
41         rdmsr
42         orl     $((1 << 9) | (1 << 19)), %eax
43         orl     $((1 << 5) | (1 << 7)), %edx
44         wrmsr
45
46         /* Zero out all Fixed Range and Variable Range MTRRs */
47         movl    $mtrr_table, %esi
48         movl    $( (mtrr_table_end - mtrr_table) / 2), %edi
49         xorl    %eax, %eax
50         xorl    %edx, %edx
51 clear_mtrrs:
52         movw    (%esi), %bx
53         movzx   %bx, %ecx
54         wrmsr
55         add     $2, %esi
56         dec     %edi
57         jnz     clear_mtrrs
58
59         /* Configure the default memory type to uncacheable */
60         movl    $MTRRdefType_MSR, %ecx
61         rdmsr
62         andl    $(~0x00000cff), %eax
63         wrmsr
64
65         /* Set cache as ram base address */
66         movl    $(MTRRphysBase_MSR(0)), %ecx
67         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
68         xorl    %edx, %edx
69         wrmsr
70
71         /* Set cache as ram mask */
72         movl    $(MTRRphysMask_MSR(0)), %ecx
73         movl    $(~((CACHE_AS_RAM_SIZE-1)) | (1 << 11)), %eax
74         movl    $0x0000000f, %edx
75         wrmsr
76
77         /* Enable MTRR */
78         movl    $MTRRdefType_MSR, %ecx
79         rdmsr
80         orl     $(1 << 11), %eax
81         wrmsr
82
83         /* Enable L2 Cache */
84         movl    $0x11e, %ecx
85         rdmsr
86         orl     $(1 << 8), %eax
87         wrmsr
88
89         /* CR0.CD = 0, CR0.NW = 0 */
90         movl    %cr0, %eax
91         andl    $( ~( (1 << 30) | (1 << 29) ) ), %eax
92         invd
93         movl    %eax, %cr0
94
95         /* Clear the cache memory reagion */
96         movl    $CACHE_AS_RAM_BASE, %esi
97         movl    %esi, %edi
98         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
99         //movl  $0x23322332, %eax
100         xorl    %eax, %eax
101         rep     stosl
102
103         /* Enable Cache As RAM mode by disabling cache */
104         movl    %cr0, %eax
105         orl     $(1 << 30), %eax
106         movl    %eax, %cr0
107
108 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
109         /* Enable cache for our code in Flash because we do XIP here */
110         movl    $MTRRphysBase_MSR(1), %ecx
111         xorl    %edx, %edx
112 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
113 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
114 #else
115 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
116 #endif
117         movl    $REAL_XIP_ROM_BASE, %eax
118         orl     $MTRR_TYPE_WRBACK, %eax
119         wrmsr
120
121         movl    $MTRRphysMask_MSR(1), %ecx
122         movl    $0x0000000f, %edx
123         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
124         wrmsr
125 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
126
127         /* enable cache */
128         movl    %cr0, %eax
129         andl    $( ~( (1 << 30) | (1 << 29) ) ), %eax
130         movl    %eax, %cr0
131
132         /* Set up stack pointer */
133 #if defined(CONFIG_USBDEBUG_DIRECT) && (CONFIG_USBDEBUG_DIRECT == 1)
134         /* leave some space for the struct ehci_debug_info */
135         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %eax
136 #else
137         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %eax
138 #endif
139         movl    %eax, %esp
140
141         /* Restore the BIST result */
142         movl    %ebp, %eax
143         movl    %esp, %ebp
144         pushl   %eax
145
146         post_code(0x23)
147
148         /* Call romstage.c main function */
149         call    main
150
151         post_code(0x2f)
152
153         post_code(0x30)
154
155         /* Disable Cache */
156         movl    %cr0, %eax
157         orl    $(1 << 30), %eax
158         movl    %eax, %cr0
159
160         post_code(0x31)
161
162         /* Disable MTRR */
163         movl    $MTRRdefType_MSR, %ecx
164         rdmsr
165         andl    $(~(1 << 11)), %eax
166         wrmsr
167
168         post_code(0x31)
169
170         invd
171 #if 0
172         xorl    %eax, %eax
173         xorl    %edx, %edx
174         movl    $MTRRphysBase_MSR(0), %ecx
175         wrmsr
176         movl    $MTRRphysMask_MSR(0), %ecx
177         wrmsr
178         movl    $MTRRphysBase_MSR(1), %ecx
179         wrmsr
180         movl    $MTRRphysMask_MSR(1), %ecx
181         wrmsr
182 #endif
183
184         post_code(0x33)
185
186 #undef CLEAR_FIRST_1M_RAM
187 #ifdef CLEAR_FIRST_1M_RAM
188         post_code(0x34)
189         /* Enable Write Combining and Speculative Reads for the first 1MB */
190         movl    $MTRRphysBase_MSR(0), %ecx
191         movl    $(0x00000000 | MTRR_TYPE_WRCOMB), %eax
192         xorl    %edx, %edx
193         wrmsr
194         movl    $MTRRphysMask_MSR(0), %ecx
195         movl    $(~(1024*1024 -1) | (1 << 11)), %eax
196         movl    $0x0000000f, %edx       // 36bit address space
197         wrmsr
198         post_code(0x35)
199 #endif
200
201         /* Enable Cache */
202         movl    %cr0, %eax
203         andl    $~( (1 << 30) | (1 << 29) ), %eax
204         movl    %eax, %cr0
205
206
207         post_code(0x36)
208 #ifdef CLEAR_FIRST_1M_RAM
209
210         /* Clear first 1MB of RAM */
211         movl    $0x00000000, %edi
212         cld
213         xorl    %eax, %eax
214         movl    $((1024*1024) / 4), %ecx
215         rep stosl
216         
217         post_code(0x37)
218 #endif
219
220         /* Disable Cache */
221         movl    %cr0, %eax
222         orl    $(1 << 30), %eax
223         movl    %eax, %cr0
224
225         post_code(0x38)
226
227         /* Enable Write Back and Speculative Reads for the first 1MB */
228         movl    $MTRRphysBase_MSR(0), %ecx
229         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
230         xorl    %edx, %edx
231         wrmsr
232         movl    $MTRRphysMask_MSR(0), %ecx
233         movl    $(~(1024*1024 -1) | (1 << 11)), %eax
234         movl    $0x0000000f, %edx       // 36bit address space
235         wrmsr
236
237         post_code(0x39)
238
239         /* And Enable Cache again after setting MTRRs */
240         movl    %cr0, %eax
241         andl    $~( (1 << 30) | (1 << 29) ), %eax
242         movl    %eax, %cr0
243
244         post_code(0x3a)
245
246         /* Enable MTRR */
247         movl    $MTRRdefType_MSR, %ecx
248         rdmsr
249         orl     $(1 << 11), %eax
250         wrmsr
251
252         post_code(0x3b)
253
254         /* Enable prefetchers */
255         movl    $0x01a0, %ecx
256         rdmsr
257         andl    $~((1 << 9) | (1 << 19)), %eax
258         andl    $~((1 << 5) | (1 << 7)), %edx
259         wrmsr
260
261         /* Invalidate the cache again */
262         invd
263
264         post_code(0x3c)
265
266         /* clear boot_complete flag */
267         xorl    %ebp, %ebp
268 __main:
269         post_code(0x11)
270         cld                     /* clear direction flag */
271         
272         movl    %ebp, %esi
273
274         movl $ROMSTAGE_STACK, %esp
275         movl    %esp, %ebp
276         pushl %esi
277         call copy_and_run
278
279 .Lhlt:  
280         post_code(0xee)
281         hlt
282         jmp     .Lhlt
283
284 mtrr_table:
285         /* Fixed MTRRs */
286         .word 0x250, 0x258, 0x259
287         .word 0x268, 0x269, 0x26A
288         .word 0x26B, 0x26C, 0x26D
289         .word 0x26E, 0x26F
290         /* Variable MTRRs */
291         .word 0x200, 0x201, 0x202, 0x203
292         .word 0x204, 0x205, 0x206, 0x207
293         .word 0x208, 0x209, 0x20A, 0x20B
294         .word 0x20C, 0x20D, 0x20E, 0x20F
295 mtrr_table_end:
296