Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / cpu / via / car / 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) 2005 Eswar Nallusamy, LANL
6  * Copyright (C) 2005 Tyan
7  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
8  * Copyright (C) 2007 coresystems GmbH
9  * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
10  * Copyright (C) 2007,2008 Carl-Daniel Hailfinger
11  * Copyright (C) 2008 VIA Technologies, Inc.
12  * (Written by Jason Zhao <jasonzhao@viatech.com.cn> for VIA)
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; version 2 of the License.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
26  */
27
28 #define CacheSize CONFIG_DCACHE_RAM_SIZE
29 #define CacheBase CONFIG_DCACHE_RAM_BASE
30
31 #include <cpu/x86/stack.h>
32 #include <cpu/x86/mtrr.h>
33
34         /* Save the BIST result */
35         movl    %eax, %ebp
36
37 CacheAsRam:
38
39         /* disable cache */
40         movl    %cr0, %eax
41         orl     $(0x1<<30),%eax
42         movl    %eax,%cr0
43         invd
44
45         /* Set the default memory type and enable fixed and variable MTRRs */
46         movl    $MTRRdefType_MSR, %ecx
47         xorl    %edx, %edx
48         /* Enable Variable and Fixed MTRRs */
49         movl    $0x00000c00, %eax
50         wrmsr
51
52         /* Clear all MTRRs */
53         xorl    %edx, %edx
54         movl    $fixed_mtrr_msr, %esi
55
56 clear_fixed_var_mtrr:
57         lodsl   (%esi), %eax
58         testl   %eax, %eax
59         jz      clear_fixed_var_mtrr_out
60
61         movl    %eax, %ecx
62         xorl    %eax, %eax
63         wrmsr
64
65         jmp     clear_fixed_var_mtrr
66
67 fixed_mtrr_msr:
68         .long   0x250, 0x258, 0x259
69         .long   0x268, 0x269, 0x26A
70         .long   0x26B, 0x26C, 0x26D
71         .long   0x26E, 0x26F
72
73 var_mtrr_msr:
74         .long   0x200, 0x201, 0x202, 0x203
75         .long   0x204, 0x205, 0x206, 0x207
76         .long   0x208, 0x209, 0x20A, 0x20B
77         .long   0x20C, 0x20D, 0x20E, 0x20F
78         .long   0x000 /* NULL, end of table */
79
80 clear_fixed_var_mtrr_out:
81         /* MTRRPhysBase */
82         movl    $0x200, %ecx
83         xorl    %edx, %edx
84         movl    $(CacheBase|MTRR_TYPE_WRBACK),%eax
85         wrmsr
86
87         /* MTRRPhysMask */
88         movl    $0x201, %ecx
89         /* This assumes we never access addresses above 2^36 in CAR. */
90         movl    $0x0000000f,%edx
91         movl    $(~(CacheSize-1)|0x800),%eax
92         wrmsr
93
94         /* enable write base caching so we can do execute in place
95          * on the flash rom.
96          */
97         /* MTRRPhysBase */
98         movl    $0x202, %ecx
99         xorl    %edx, %edx
100 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
101 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
102 #else
103 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
104 #endif
105         movl    $REAL_XIP_ROM_BASE, %eax
106         orl     $MTRR_TYPE_WRBACK, %eax
107         wrmsr
108
109         /* MTRRPhysMask */
110         movl    $0x203, %ecx
111         movl    $0x0000000f, %edx
112         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
113         wrmsr
114
115         movl    $MTRRdefType_MSR, %ecx
116         xorl    %edx, %edx
117         /* Enable Variable and Fixed MTRRs */
118         movl    $0x00000800, %eax
119         wrmsr
120
121         movl    %cr0, %eax
122         andl    $0x9fffffff, %eax
123         movl    %eax, %cr0
124
125         /* Read the range with lodsl*/
126         cld
127         movl    $CacheBase, %esi
128         movl    %esi, %edi
129         movl    $(CacheSize>>2), %ecx
130         rep     lodsl
131
132         movl    $CacheBase, %esi
133         movl    %esi, %edi
134         movl    $(CacheSize >> 2), %ecx
135
136         /* 0x5c5c5c5c is a memory test pattern.
137          * TODO: Check if everything works with the zero pattern as well. */
138         /*xorl  %eax, %eax*/
139         xorl    $0x5c5c5c5c,%eax
140         rep     stosl
141
142         movl    REAL_XIP_ROM_BASE, %esi
143         movl    %esi, %edi
144         movl    $(CONFIG_XIP_ROM_SIZE>>2), %ecx
145         rep     lodsl
146
147         /* The key point of this CAR code is C7 cache does not turn into
148          * "no fill" mode, which is not compatible with general CAR code.
149          */
150
151         movl    $(CacheBase + CacheSize - 4), %eax
152         movl    %eax, %esp
153
154 #ifdef CARTEST
155 testok: movb $0x40,%al
156         outb %al, $0x80
157         xorl    %edx, %edx
158         xorl    %eax, %eax
159         movl    $0x5c5c,%edx
160         pushl %edx
161         pushl %edx
162         pushl %edx
163         pushl %edx
164         pushl %edx
165         popl    %esi
166         popl    %esi
167         popl    %eax
168         popl    %eax
169         popl    %eax
170         cmpl %edx,%eax
171         jne stackerr
172 #endif
173
174         /* Restore the BIST result */
175         movl    %ebp, %eax
176
177         /* We need to set ebp ? No need */
178         movl    %esp, %ebp
179         pushl   %eax  /* bist */
180         call    main
181
182         /*
183          * TODO: Backup stack in CACHE_AS_RAM into MMX and SSE and after we
184          *       get STACK up, we restore that. It is only needed if we
185          *       want to go back.
186          */
187
188         /* We don't need cache as ram for now on */
189         /* disable cache */
190         movl    %cr0, %eax
191         orl    $(0x1<<30),%eax
192         movl    %eax, %cr0
193
194
195         /* Set the default memory type and disable fixed and enable variable MTRRs */
196         movl    $0x2ff, %ecx
197         //movl    $MTRRdefType_MSR, %ecx
198         xorl    %edx, %edx
199
200         /* Enable Variable and Disable Fixed MTRRs */
201         movl    $0x00000800, %eax
202         wrmsr
203
204         /* enable caching for first 1M using variable mtrr */
205         movl    $0x200, %ecx
206         xorl    %edx, %edx
207         movl     $(0 | 6), %eax
208         //movl     $(0 | MTRR_TYPE_WRBACK), %eax
209         wrmsr
210
211         /* enable cache for 0-7ffff, 80000-9ffff, e0000-fffff;
212          * If 1M cacheable, then when S3 resume, there is stange color on
213          * screen for 2 sec. suppose problem of a0000-dfffff and cache.
214          * And in x86_setup_fixed_mtrrs()(mtrr.c), 0-256M is set cacheable.
215          */
216
217         movl    $0x201, %ecx
218         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
219         movl    $((~(( 0 + 0x80000) - 1)) | 0x800), %eax
220         wrmsr
221
222         movl    $0x202, %ecx
223         xorl    %edx, %edx
224         movl     $(0x80000 | 6), %eax
225         orl     $(0 | 6), %eax
226         wrmsr
227
228         movl    $0x203, %ecx
229         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
230         movl    $((~(( 0 + 0x20000) - 1)) | 0x800), %eax
231         wrmsr
232
233         movl    $0x204, %ecx
234         xorl    %edx, %edx
235         movl     $(0xc0000 | 6), %eax
236         orl     $(0 | 6), %eax
237         wrmsr
238
239         movl    $0x205, %ecx
240         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
241         movl    $((~(( 0 + 0x40000) - 1)) | 0x800), %eax
242         wrmsr
243
244         /* cache XIP_ROM_BASE-SIZE to speedup coreboot code */
245         movl    $0x206, %ecx
246         xorl    %edx, %edx
247         movl     $REAL_XIP_ROM_BASE,%eax
248         orl     $(0 | 6), %eax
249         wrmsr
250
251         movl    $0x207, %ecx
252         xorl    %edx, %edx
253         movl     $CONFIG_XIP_ROM_SIZE,%eax
254         decl    %eax
255         notl    %eax
256         orl     $(0 | 0x800), %eax
257         wrmsr
258
259         /* enable cache */
260         movl    %cr0, %eax
261         andl    $0x9fffffff,%eax
262         movl    %eax, %cr0
263         invd
264
265         /* clear boot_complete flag */
266         xorl    %ebp, %ebp
267 __main:
268         post_code(0x11)
269         cld                     /* clear direction flag */
270
271         movl    %ebp, %esi
272
273         movl $ROMSTAGE_STACK, %esp
274         movl    %esp, %ebp
275         pushl %esi
276         call copy_and_run
277
278 .Lhlt:
279         post_code(0xee)
280         hlt
281         jmp     .Lhlt
282