Factor out common CAR asm snippets.
[coreboot.git] / src / cpu / intel / model_106cx / 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 #include <cpu/x86/car.h>
22 #include <cpu/x86/stack.h>
23 #include <cpu/x86/mtrr.h>
24
25 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
26 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
27
28         save_bist_result()
29
30 cache_as_ram:
31         post_code(0x20)
32
33         /* Send INIT IPI to all excluding ourself. */
34         movl    $0x000C4500, %eax
35         movl    $0xFEE00300, %esi
36         movl    %eax, (%esi)
37
38         /* Zero out all fixed range and variable range MTRRs. */
39         movl    $mtrr_table, %esi
40         movl    $((mtrr_table_end - mtrr_table) / 2), %edi
41         xorl    %eax, %eax
42         xorl    %edx, %edx
43 clear_mtrrs:
44         movw    (%esi), %bx
45         movzx   %bx, %ecx
46         wrmsr
47         add     $2, %esi
48         dec     %edi
49         jnz     clear_mtrrs
50
51         /* Configure the default memory type to uncacheable. */
52         movl    $MTRRdefType_MSR, %ecx
53         rdmsr
54         andl    $(~0x00000cff), %eax
55         wrmsr
56
57         /* Set Cache-as-RAM base address. */
58         movl    $(MTRRphysBase_MSR(0)), %ecx
59         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
60         xorl    %edx, %edx
61         wrmsr
62
63         /* Set Cache-as-RAM mask. */
64         movl    $(MTRRphysMask_MSR(0)), %ecx
65         movl    $(~((CACHE_AS_RAM_SIZE - 1)) | (1 << 11)), %eax
66         xorl    %edx, %edx
67         wrmsr
68
69         enable_mtrr()
70
71         enable_l2_cache()
72
73         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
74         /* TODO: enable_cache()? But that doesn't have "invd". */
75         movl    %cr0, %eax
76         andl    $(~((1 << 30) | (1 << 29))), %eax
77         invd
78         movl    %eax, %cr0
79
80         /* Clear the cache memory reagion. */
81         movl    $CACHE_AS_RAM_BASE, %esi
82         movl    %esi, %edi
83         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
84         // movl $0x23322332, %eax
85         xorl    %eax, %eax
86         rep     stosl
87
88         /* Enable Cache-as-RAM mode by disabling cache. */
89         disable_cache()
90
91 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
92         /* Enable cache for our code in Flash because we do XIP here */
93         movl    $MTRRphysBase_MSR(1), %ecx
94         xorl    %edx, %edx
95 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
96 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
97 #else
98 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
99 #endif
100         movl    $REAL_XIP_ROM_BASE, %eax
101         orl     $MTRR_TYPE_WRBACK, %eax
102         wrmsr
103
104         movl    $MTRRphysMask_MSR(1), %ecx
105         xorl    %edx, %edx
106         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
107         wrmsr
108 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
109
110         enable_cache()
111
112         /* Set up the stack pointer. */
113 #if defined(CONFIG_USBDEBUG) && (CONFIG_USBDEBUG == 1)
114         /* Leave some space for the struct ehci_debug_info. */
115         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %eax
116 #else
117         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %eax
118 #endif
119         movl    %eax, %esp
120
121         restore_bist_result()
122
123         movl    %esp, %ebp
124         pushl   %eax
125
126         post_code(0x23)
127
128         /* Call romstage.c main function. */
129         call    main
130
131         post_code(0x2f)
132
133         post_code(0x30)
134
135         disable_cache()
136
137         post_code(0x31)
138
139         disable_mtrr()
140
141         post_code(0x31)
142
143         invd
144 #if 0
145         xorl    %eax, %eax
146         xorl    %edx, %edx
147         movl    $MTRRphysBase_MSR(0), %ecx
148         wrmsr
149         movl    $MTRRphysMask_MSR(0), %ecx
150         wrmsr
151         movl    $MTRRphysBase_MSR(1), %ecx
152         wrmsr
153         movl    $MTRRphysMask_MSR(1), %ecx
154         wrmsr
155 #endif
156
157         post_code(0x33)
158
159         enable_cache()
160
161         post_code(0x36)
162
163         disable_cache()
164
165         post_code(0x38)
166
167         /* Enable Write Back and Speculative Reads for the first 1MB. */
168         movl    $MTRRphysBase_MSR(0), %ecx
169         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
170         xorl    %edx, %edx
171         wrmsr
172         movl    $MTRRphysMask_MSR(0), %ecx
173         movl    $(~(1024 * 1024 - 1) | (1 << 11)), %eax
174         xorl    %edx, %edx
175         wrmsr
176
177         post_code(0x39)
178
179         /* And enable cache again after setting MTRRs. */
180         enable_cache()
181
182         post_code(0x3a)
183
184         enable_mtrr()
185
186         post_code(0x3b)
187
188         /* Invalidate the cache again. */
189         invd
190
191         post_code(0x3c)
192
193         /* Clear boot_complete flag. */
194         xorl    %ebp, %ebp
195 __main:
196         post_code(0x11)
197         cld                     /* Clear direction flag. */
198
199         movl    %ebp, %esi
200
201         movl    $ROMSTAGE_STACK, %esp
202         movl    %esp, %ebp
203         pushl   %esi
204         call    copy_and_run
205
206 .Lhlt:
207         post_code(0xee)
208         hlt
209         jmp     .Lhlt
210
211 mtrr_table:
212         /* Fixed MTRRs */
213         .word 0x250, 0x258, 0x259
214         .word 0x268, 0x269, 0x26A
215         .word 0x26B, 0x26C, 0x26D
216         .word 0x26E, 0x26F
217         /* Variable MTRRs */
218         .word 0x200, 0x201, 0x202, 0x203
219         .word 0x204, 0x205, 0x206, 0x207
220         .word 0x208, 0x209, 0x20A, 0x20B
221         .word 0x20C, 0x20D, 0x20E, 0x20F
222 mtrr_table_end:
223