Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / northbridge / via / vx800 / dram_util.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 One Laptop per Child, Association, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 void WaitMicroSec(UINTN MicroSeconds)
21 {
22         u32 i;
23
24         for (i = 0; i < 1024 * MicroSeconds; i++) {
25                 __asm__ volatile ("nop\n\t");
26         }
27
28         return;
29 }
30
31 /*===================================================================
32 Function   : via_write_phys()
33 Precondition :
34 Input      :  addr
35                   value
36 Output     : void
37 Purpose    :
38 Reference  : None
39 ===================================================================*/
40
41 void via_write_phys(volatile u32 addr, volatile u32 value)
42 {
43         volatile u32 *ptr;
44         ptr = (volatile u32 *)addr;
45         *ptr = (volatile u32)value;
46 }
47
48 /*===================================================================
49 Function   : via_read_phys()
50 Precondition :
51 Input      :  addr
52 Output     : u32
53 Purpose    :
54 Reference  : None
55 ===================================================================*/
56
57 u32 via_read_phys(volatile u32 addr)
58 {
59         volatile u32 y;
60         y = *(volatile u32 *)addr;
61         return y;
62 }
63
64 /*===================================================================
65 Function   : DimmRead()
66 Precondition :
67 Input      :  x
68 Output     : u32
69 Purpose    :
70 Reference  : None
71 ===================================================================*/
72
73 u32 DimmRead(volatile u32 x)
74 {                               //  volatile u32 z;
75         volatile u32 y;
76         y = *(volatile u32 *)x;
77
78         return y;
79 }
80
81 /*===================================================================
82 Function   : DramBaseTest()
83 Precondition : this function used to verify memory
84 Input      :
85                  BaseAdd,
86                  length,
87                  mode
88 Output     : u32
89 Purpose    :write into and read out to verify if dram is correct
90 Reference  : None
91 ===================================================================*/
92 BOOLEAN DramBaseTest(u32 BaseAdd, u32 Length,
93                      DRAM_TEST_MODE Mode, BOOLEAN PrintFlag)
94 {
95         u32 TestSpan;
96         u32 Data, Address, Address2;
97         u8 i, TestCount;
98
99         //decide the test mode is continous or step
100         if (Mode == EXTENSIVE) {
101                 //the test mode is continuos and must test each unit
102                 TestSpan = 4;
103                 TestCount = 1;
104         } else if (Mode == SPARE) {
105                 // the test mode is step and test some unit
106                 TestSpan = STEPSPAN;
107                 TestCount = TESTCOUNT;
108         } else {
109                 PRINT_DEBUG_MEM("the test mode is error\r");
110                 return FALSE;
111         }
112
113         //write each test unit the value with TEST_PATTERN
114         for (Address = BaseAdd; Address < BaseAdd + Length; Address += TestSpan) {
115                 for (i = 0; i < TestCount; i++)
116                         via_write_phys(Address + i * 4, TEST_PATTERN);
117                 if (PrintFlag) {
118                         if ((u32) Address % 0x10000000 == 0) {
119                                 PRINT_DEBUG_MEM("Write in Addr =");
120                                 PRINT_DEBUG_MEM_HEX32(Address);
121                                 PRINT_DEBUG_MEM("\r");
122                         }
123                 }
124         }
125
126         //compare each test unit with the value of TEST_PATTERN
127         //and write it with compliment of TEST_PATTERN
128         for (Address = BaseAdd; Address < BaseAdd + Length; Address += TestSpan) {
129                 for (i = 0; i < TestCount; i++) {
130                         Data = via_read_phys(Address + i * 4);
131                         via_write_phys(Address + i * 4, (u32) (~TEST_PATTERN));
132                         if (Data != TEST_PATTERN) {
133                                 PRINT_DEBUG_MEM("TEST_PATTERN ERROR !!!!! ");
134                                 Address2 = Address + i * 4;
135                                 PRINT_DEBUG_MEM_HEX32(Address2);
136                                 PRINT_DEBUG_MEM(" : ");
137                                 PRINT_DEBUG_MEM_HEX32(Data);
138                                 PRINT_DEBUG_MEM(" \r");
139                                 return FALSE;
140                         }
141                 }
142                 if (PrintFlag) {
143                         if ((u32) Address % 0x10000000 == 0) {
144                                 PRINT_DEBUG_MEM("Write in Addr =");
145                                 PRINT_DEBUG_MEM_HEX32(Address);
146                                 PRINT_DEBUG_MEM("\r");
147                         }
148                 }
149         }
150
151         //compare each test unit with the value of ~TEST_PATTERN
152         for (Address = BaseAdd; Address < BaseAdd + Length; Address += TestSpan) {
153                 for (i = (u8) (TestCount); i > 0; i--) {
154                         Data = via_read_phys(Address + (i - 1) * 4);
155                         if (Data != ~TEST_PATTERN) {
156
157                                 PRINT_DEBUG_MEM("~TEST_PATTERN ERROR !!!!! ");
158                                 Address2 = Address + (i - 1) * 4;
159                                 PRINT_DEBUG_MEM_HEX32(Address2);
160                                 PRINT_DEBUG_MEM(" : ");
161                                 PRINT_DEBUG_MEM_HEX32(Data);
162                                 PRINT_DEBUG_MEM(" \r");
163                                 return FALSE;
164                         }
165                 }
166         }
167
168         return TRUE;
169 }
170
171 /*===================================================================
172 Function   : DumpRegisters()
173 Precondition :
174 Input      :
175                 pPCIPPI,
176                 DevNum,
177                 FuncNum
178 Output     : Void
179 Purpose    :
180 Reference  : None
181 ===================================================================*/
182
183 void DumpRegisters(INTN DevNum, INTN FuncNum)
184 {
185         INTN i, j;
186         u8 ByteVal;
187
188         ByteVal = 0;
189         //pci_write_config8(PCI_DEV(0, DevNum, FuncNum), 0xA1, ByteVal);
190         PRINT_DEBUG_MEM("\rDev %02x Fun %02x\r");
191         PRINT_DEBUG_MEM
192             ("\r    00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\r");
193         PRINT_DEBUG_MEM
194             ("---------------------------------------------------\r");
195         for (i = 0; i < 0x10; i++) {
196                 PRINT_DEBUG_MEM_HEX32(i);
197                 for (j = 0; j < 0x10; j++) {
198                         ByteVal =
199                             pci_read_config8(PCI_DEV(0, DevNum, FuncNum),
200                                              i * 0x10 + j);
201                         PRINT_DEBUG_MEM_HEX8(ByteVal);
202                         PRINT_DEBUG_MEM(" ");
203
204                 }
205                 PRINT_DEBUG_MEM("\r");
206         }
207         return;
208 }
209
210 /*===================================================================
211 Function   : dumpnorth()
212 Precondition :
213 Input      :
214                 pPCIPPI,
215                 Func
216 Output     : Void
217 Purpose    :
218 Reference  : None
219 ===================================================================*/
220
221 void dumpnorth(u8 Func)
222 {
223         u16 r, c;
224         u8 ByteVal;
225         PRINT_DEBUG_MEM("Dump North!!!\r");
226         for (r = 0; r < 32; r++) {
227                 for (c = (u16) (r << 3); c < (r << 3) + 8; c++) {
228                         ByteVal = 0;
229                         ByteVal = pci_read_config8(PCI_DEV(0, 0, Func), c);
230                         PRINT_DEBUG_MEM_HEX16(c);
231                         PRINT_DEBUG_MEM("= ");
232                         PRINT_DEBUG_MEM_HEX8(ByteVal);
233                 }
234                 PRINT_DEBUG_MEM("\r");
235         }
236 }