Eliminate SET_NB_CFG_54 option. There was no board that
[coreboot.git] / src / cpu / amd / model_gx2 / cache_as_ram.inc
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
5  * Copyright (C) 2010 Nils Jacobs
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 version 2 as
9  * published by the Free Software Foundation.
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 GX2_STACK_BASE          CONFIG_DCACHE_RAM_BASE          /* this is where the DCache will be mapped and be used as stack, It would be cool if it was the same base as coreboot normal stack */
22 #define GX2_STACK_END           GX2_STACK_BASE+(CONFIG_DCACHE_RAM_SIZE-1)
23
24 #define GX2_NUM_CACHELINES      0x080   /* there are 128lines per way */
25 #define GX2_CACHELINE_SIZE      0x020   /* there are 32bytes per line */
26 #define GX2_CACHEWAY_SIZE       (GX2_NUM_CACHELINES * GX2_CACHELINE_SIZE)
27 #define CR0_CD                          0x40000000      /* bit 30 = Cache Disable */
28 #define CR0_NW                          0x20000000      /* bit 29 = Not Write Through */
29 #include <cpu/amd/gx2def.h>
30 /***************************************************************************
31 /**
32 /**     DCacheSetup
33 /**
34 /**     Setup data cache for  use as RAM for a stack.
35 /**
36 /**     Max. size data cache =0x4000 (16KB)
37 /**
38 /***************************************************************************/
39 DCacheSetup:
40         /* Save the BIST result */
41         movl    %eax, %ebx
42
43         invd
44         /* set cache properties */
45         movl    $CPU_RCONF_DEFAULT, %ecx
46         rdmsr
47         movl    $0x010010000, %eax              /*1MB system memory in write back 1|00100|00 */
48         wrmsr
49
50         /* in GX2 DCDIS is set after POR which disables the cache..., clear this bit */
51         movl    $CPU_DM_CONFIG0, %ecx
52         rdmsr
53         andl    $(~(DM_CONFIG0_LOWER_DCDIS_SET)), %eax  /* TODO: make consistent with i$ init,  either whole reg = 0,  or just this bit... */
54         wrmsr
55
56         /* Get cleaned up. */
57         xorl    %edi, %edi
58         xorl    %esi, %esi
59         xorl    %ebp, %ebp
60
61         /* DCache Ways0 through Ways3 will be tagged for GX2_STACK_BASE + CONFIG_DCACHE_RAM_SIZE for holding stack */
62         /* remember,  there is NO stack yet... */
63
64         /* Tell cache we want to fill WAY 0 starting at the top */
65         xorl    %edx, %edx
66         xorl    %eax, %eax
67         movl    $CPU_DC_INDEX, %ecx
68         wrmsr
69
70         /* startaddress for tag of Way0: ebp will hold the incrementing address. dont destroy! */
71         movl    $GX2_STACK_BASE, %ebp   /* init to start address */
72         orl     $1, %ebp        /* set valid bit and tag for this Way (B[31:12] : Cache tag value for line/way curr. selected by CPU_DC_INDEX */
73
74         /* start tag Ways 0 with 128 lines with 32bytes each: edi will hold the line counter. dont destroy! */
75         movl    $GX2_NUM_CACHELINES, %edi
76 DCacheSetupFillWay:
77
78         /* fill with dummy data: zero it so we can tell it from PCI memory space (returns FFs). */
79         /* We will now store a line (32 bytes = 4 x 8bytes = 4 quadWords) */
80         movw    $0x04, %si
81         xorl    %edx, %edx
82         xorl    %eax, %eax
83         movl    $CPU_DC_DATA, %ecx
84 DCacheSetup_quadWordLoop:
85         wrmsr
86         decw    %si
87         jnz     DCacheSetup_quadWordLoop
88
89         /* Set the tag for this line,need to do this for every new cache line to validate it! */
90         /* accessing CPU_DC_TAG_I makes the LINE field in CPU_DC_INDEX increment and thus cont. in the next cache line... */
91         xorl    %edx, %edx
92         movl    %ebp, %eax
93         movl    $CPU_DC_TAG, %ecx
94         wrmsr
95
96         /* switch to next line */
97         /* lines are in Bits8:2 */
98         /* when index is crossing 0x7F -> 0x80  writing a RSVD bit as 0x80 is not a valid CL anymore! */
99         movl    $CPU_DC_INDEX, %ecx
100         rdmsr
101         addl    $0x04, %eax /* inc DC_LINE. TODO: prob. would be more elegant to calc. this from counter var edi... */
102         wrmsr
103
104         decl    %edi
105         jnz     DCacheSetupFillWay
106
107         /* 1 Way has been filled,  forward start address for next Way,  terminate if we have reached end of desired address range */
108         addl    $GX2_CACHEWAY_SIZE, %ebp
109         cmpl    $GX2_STACK_END, %ebp
110         jge     leave_DCacheSetup
111         movl    $GX2_NUM_CACHELINES, %edi
112
113         /* switch to next way */
114         movl    $CPU_DC_INDEX, %ecx
115         rdmsr
116         addl    $0x01, %eax
117         andl    $0xFFFFFE03, %eax /* lets be sure: reset line index Bits8:2 */
118         wrmsr
119
120         jmp     DCacheSetupFillWay
121
122 leave_DCacheSetup:
123         xorl    %edi, %edi
124         xorl    %esi, %esi
125         xorl    %ebp, %ebp
126
127         /* Disable the cache,  but ... DO NOT INVALIDATE the tags. */
128         /* Memory reads and writes will all hit in the cache. */
129         /* Cache updates and memory write-backs will not occur ! */
130         movl    %cr0, %eax
131         orl             $(CR0_CD + CR0_NW), %eax        /* set the CD and NW bits */
132         movl    %eax, %cr0
133
134         /* Now point sp to the cached stack. */
135         /* The stack will be fully functional at this location. No system memory is required at all ! */
136         /* set up the stack pointer */
137         movl    $GX2_STACK_END, %eax
138         movl    %eax, %esp
139
140         /* test the stack*/
141         movl    $0x0F0F05A5A, %edx
142         pushl   %edx
143         popl    %ecx
144         cmpl    %ecx, %edx
145         je      DCacheSetupGood
146
147         post_code(0xc5)
148 DCacheSetupBad:
149         hlt             /* issues */
150         jmp DCacheSetupBad
151 DCacheSetupGood:
152         /* Go do early init and memory setup */
153
154         /* Restore the BIST result */
155         movl    %ebx, %eax
156         movl    %esp, %ebp
157         pushl   %eax
158
159         post_code(0x23)
160
161         /* Call romstage.c main function */
162         call    main
163 done_cache_as_ram_main:
164
165         /* We now run over the stack-in-cache, copying it back to itself to invalidate the cache */
166
167         push   %edi
168         mov    $(CONFIG_DCACHE_RAM_SIZE/4),%ecx
169         push   %esi
170         mov    $(CONFIG_DCACHE_RAM_BASE),%edi
171         mov    %edi,%esi
172         cld
173         rep movsl %ds:(%esi),%es:(%edi)
174         pop    %esi
175         pop    %edi
176
177         /* Clear the cache out to ram */
178         wbinvd
179         /* re-enable the cache */
180         movl    %cr0, %eax
181         xorl             $(CR0_CD + CR0_NW), %eax        /* clear  the CD and NW bits */
182         movl    %eax, %cr0
183
184         /* clear boot_complete flag */
185         xorl    %ebp, %ebp
186 __main:
187         post_code(0x11)
188
189         /* TODO For suspend/resume the cache will have to live between
190          * CONFIG_RAMBASE and CONFIG_RAMTOP
191          */
192
193         cld                             /* clear direction flag */
194
195         /* copy coreboot from it's initial load location to
196          * the location it is compiled to run at.
197          * Normally this is copying from FLASH ROM to RAM.
198          */
199         movl    %ebp, %esi
200         pushl   %esi
201         call copy_and_run
202
203 .Lhlt:
204         post_code(0xee)
205         hlt
206         jmp     .Lhlt
207