xref: /OK3568_Linux_fs/u-boot/post/drivers/memory.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2002
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* Memory test
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * General observations:
13*4882a593Smuzhiyun  * o The recommended test sequence is to test the data lines: if they are
14*4882a593Smuzhiyun  *   broken, nothing else will work properly.  Then test the address
15*4882a593Smuzhiyun  *   lines.  Finally, test the cells in the memory now that the test
16*4882a593Smuzhiyun  *   program knows that the address and data lines work properly.
17*4882a593Smuzhiyun  *   This sequence also helps isolate and identify what is faulty.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * o For the address line test, it is a good idea to use the base
20*4882a593Smuzhiyun  *   address of the lowest memory location, which causes a '1' bit to
21*4882a593Smuzhiyun  *   walk through a field of zeros on the address lines and the highest
22*4882a593Smuzhiyun  *   memory location, which causes a '0' bit to walk through a field of
23*4882a593Smuzhiyun  *   '1's on the address line.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * o Floating buses can fool memory tests if the test routine writes
26*4882a593Smuzhiyun  *   a value and then reads it back immediately.  The problem is, the
27*4882a593Smuzhiyun  *   write will charge the residual capacitance on the data bus so the
28*4882a593Smuzhiyun  *   bus retains its state briefely.  When the test program reads the
29*4882a593Smuzhiyun  *   value back immediately, the capacitance of the bus can allow it
30*4882a593Smuzhiyun  *   to read back what was written, even though the memory circuitry
31*4882a593Smuzhiyun  *   is broken.  To avoid this, the test program should write a test
32*4882a593Smuzhiyun  *   pattern to the target location, write a different pattern elsewhere
33*4882a593Smuzhiyun  *   to charge the residual capacitance in a differnt manner, then read
34*4882a593Smuzhiyun  *   the target location back.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * o Always read the target location EXACTLY ONCE and save it in a local
37*4882a593Smuzhiyun  *   variable.  The problem with reading the target location more than
38*4882a593Smuzhiyun  *   once is that the second and subsequent reads may work properly,
39*4882a593Smuzhiyun  *   resulting in a failed test that tells the poor technician that
40*4882a593Smuzhiyun  *   "Memory error at 00000000, wrote aaaaaaaa, read aaaaaaaa" which
41*4882a593Smuzhiyun  *   doesn't help him one bit and causes puzzled phone calls.  Been there,
42*4882a593Smuzhiyun  *   done that.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * Data line test:
45*4882a593Smuzhiyun  * ---------------
46*4882a593Smuzhiyun  * This tests data lines for shorts and opens by forcing adjacent data
47*4882a593Smuzhiyun  * to opposite states. Because the data lines could be routed in an
48*4882a593Smuzhiyun  * arbitrary manner the must ensure test patterns ensure that every case
49*4882a593Smuzhiyun  * is tested. By using the following series of binary patterns every
50*4882a593Smuzhiyun  * combination of adjacent bits is test regardless of routing.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  *     ...101010101010101010101010
53*4882a593Smuzhiyun  *     ...110011001100110011001100
54*4882a593Smuzhiyun  *     ...111100001111000011110000
55*4882a593Smuzhiyun  *     ...111111110000000011111111
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * Carrying this out, gives us six hex patterns as follows:
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  *     0xaaaaaaaaaaaaaaaa
60*4882a593Smuzhiyun  *     0xcccccccccccccccc
61*4882a593Smuzhiyun  *     0xf0f0f0f0f0f0f0f0
62*4882a593Smuzhiyun  *     0xff00ff00ff00ff00
63*4882a593Smuzhiyun  *     0xffff0000ffff0000
64*4882a593Smuzhiyun  *     0xffffffff00000000
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * To test for short and opens to other signals on our boards, we
67*4882a593Smuzhiyun  * simply test with the 1's complemnt of the paterns as well, resulting
68*4882a593Smuzhiyun  * in twelve patterns total.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * After writing a test pattern. a special pattern 0x0123456789ABCDEF is
71*4882a593Smuzhiyun  * written to a different address in case the data lines are floating.
72*4882a593Smuzhiyun  * Thus, if a byte lane fails, you will see part of the special
73*4882a593Smuzhiyun  * pattern in that byte lane when the test runs.  For example, if the
74*4882a593Smuzhiyun  * xx__xxxxxxxxxxxx byte line fails, you will see aa23aaaaaaaaaaaa
75*4882a593Smuzhiyun  * (for the 'a' test pattern).
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * Address line test:
78*4882a593Smuzhiyun  * ------------------
79*4882a593Smuzhiyun  *  This function performs a test to verify that all the address lines
80*4882a593Smuzhiyun  *  hooked up to the RAM work properly.  If there is an address line
81*4882a593Smuzhiyun  *  fault, it usually shows up as two different locations in the address
82*4882a593Smuzhiyun  *  map (related by the faulty address line) mapping to one physical
83*4882a593Smuzhiyun  *  memory storage location.  The artifact that shows up is writing to
84*4882a593Smuzhiyun  *  the first location "changes" the second location.
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * To test all address lines, we start with the given base address and
87*4882a593Smuzhiyun  * xor the address with a '1' bit to flip one address line.  For each
88*4882a593Smuzhiyun  * test, we shift the '1' bit left to test the next address line.
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * In the actual code, we start with address sizeof(ulong) since our
91*4882a593Smuzhiyun  * test pattern we use is a ulong and thus, if we tried to test lower
92*4882a593Smuzhiyun  * order address bits, it wouldn't work because our pattern would
93*4882a593Smuzhiyun  * overwrite itself.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * Example for a 4 bit address space with the base at 0000:
96*4882a593Smuzhiyun  *   0000 <- base
97*4882a593Smuzhiyun  *   0001 <- test 1
98*4882a593Smuzhiyun  *   0010 <- test 2
99*4882a593Smuzhiyun  *   0100 <- test 3
100*4882a593Smuzhiyun  *   1000 <- test 4
101*4882a593Smuzhiyun  * Example for a 4 bit address space with the base at 0010:
102*4882a593Smuzhiyun  *   0010 <- base
103*4882a593Smuzhiyun  *   0011 <- test 1
104*4882a593Smuzhiyun  *   0000 <- (below the base address, skipped)
105*4882a593Smuzhiyun  *   0110 <- test 2
106*4882a593Smuzhiyun  *   1010 <- test 3
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * The test locations are successively tested to make sure that they are
109*4882a593Smuzhiyun  * not "mirrored" onto the base address due to a faulty address line.
110*4882a593Smuzhiyun  * Note that the base and each test location are related by one address
111*4882a593Smuzhiyun  * line flipped.  Note that the base address need not be all zeros.
112*4882a593Smuzhiyun  *
113*4882a593Smuzhiyun  * Memory tests 1-4:
114*4882a593Smuzhiyun  * -----------------
115*4882a593Smuzhiyun  * These tests verify RAM using sequential writes and reads
116*4882a593Smuzhiyun  * to/from RAM. There are several test cases that use different patterns to
117*4882a593Smuzhiyun  * verify RAM. Each test case fills a region of RAM with one pattern and
118*4882a593Smuzhiyun  * then reads the region back and compares its contents with the pattern.
119*4882a593Smuzhiyun  * The following patterns are used:
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  *  1a) zero pattern (0x00000000)
122*4882a593Smuzhiyun  *  1b) negative pattern (0xffffffff)
123*4882a593Smuzhiyun  *  1c) checkerboard pattern (0x55555555)
124*4882a593Smuzhiyun  *  1d) checkerboard pattern (0xaaaaaaaa)
125*4882a593Smuzhiyun  *  2)  bit-flip pattern ((1 << (offset % 32))
126*4882a593Smuzhiyun  *  3)  address pattern (offset)
127*4882a593Smuzhiyun  *  4)  address pattern (~offset)
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * Being run in normal mode, the test verifies only small 4Kb
130*4882a593Smuzhiyun  * regions of RAM around each 1Mb boundary. For example, for 64Mb
131*4882a593Smuzhiyun  * RAM the following areas are verified: 0x00000000-0x00000800,
132*4882a593Smuzhiyun  * 0x000ff800-0x00100800, 0x001ff800-0x00200800, ..., 0x03fff800-
133*4882a593Smuzhiyun  * 0x04000000. If the test is run in slow-test mode, it verifies
134*4882a593Smuzhiyun  * the whole RAM.
135*4882a593Smuzhiyun  */
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #include <post.h>
138*4882a593Smuzhiyun #include <watchdog.h>
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun #if CONFIG_POST & (CONFIG_SYS_POST_MEMORY | CONFIG_SYS_POST_MEM_REGIONS)
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun  * Define INJECT_*_ERRORS for testing error detection in the presence of
146*4882a593Smuzhiyun  * _good_ hardware.
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun #undef  INJECT_DATA_ERRORS
149*4882a593Smuzhiyun #undef  INJECT_ADDRESS_ERRORS
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #ifdef INJECT_DATA_ERRORS
152*4882a593Smuzhiyun #warning "Injecting data line errors for testing purposes"
153*4882a593Smuzhiyun #endif
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun #ifdef INJECT_ADDRESS_ERRORS
156*4882a593Smuzhiyun #warning "Injecting address line errors for testing purposes"
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * This function performs a double word move from the data at
162*4882a593Smuzhiyun  * the source pointer to the location at the destination pointer.
163*4882a593Smuzhiyun  * This is helpful for testing memory on processors which have a 64 bit
164*4882a593Smuzhiyun  * wide data bus.
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * On those PowerPC with FPU, use assembly and a floating point move:
167*4882a593Smuzhiyun  * this does a 64 bit move.
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * For other processors, let the compiler generate the best code it can.
170*4882a593Smuzhiyun  */
move64(const unsigned long long * src,unsigned long long * dest)171*4882a593Smuzhiyun static void move64(const unsigned long long *src, unsigned long long *dest)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	*dest = *src;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  * This is 64 bit wide test patterns.  Note that they reside in ROM
178*4882a593Smuzhiyun  * (which presumably works) and the tests write them to RAM which may
179*4882a593Smuzhiyun  * not work.
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * The "otherpattern" is written to drive the data bus to values other
182*4882a593Smuzhiyun  * than the test pattern.  This is for detecting floating bus lines.
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  */
185*4882a593Smuzhiyun const static unsigned long long pattern[] = {
186*4882a593Smuzhiyun 	0xaaaaaaaaaaaaaaaaULL,
187*4882a593Smuzhiyun 	0xccccccccccccccccULL,
188*4882a593Smuzhiyun 	0xf0f0f0f0f0f0f0f0ULL,
189*4882a593Smuzhiyun 	0xff00ff00ff00ff00ULL,
190*4882a593Smuzhiyun 	0xffff0000ffff0000ULL,
191*4882a593Smuzhiyun 	0xffffffff00000000ULL,
192*4882a593Smuzhiyun 	0x00000000ffffffffULL,
193*4882a593Smuzhiyun 	0x0000ffff0000ffffULL,
194*4882a593Smuzhiyun 	0x00ff00ff00ff00ffULL,
195*4882a593Smuzhiyun 	0x0f0f0f0f0f0f0f0fULL,
196*4882a593Smuzhiyun 	0x3333333333333333ULL,
197*4882a593Smuzhiyun 	0x5555555555555555ULL
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun const unsigned long long otherpattern = 0x0123456789abcdefULL;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 
memory_post_dataline(unsigned long long * pmem)202*4882a593Smuzhiyun static int memory_post_dataline(unsigned long long * pmem)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	unsigned long long temp64 = 0;
205*4882a593Smuzhiyun 	int num_patterns = ARRAY_SIZE(pattern);
206*4882a593Smuzhiyun 	int i;
207*4882a593Smuzhiyun 	unsigned int hi, lo, pathi, patlo;
208*4882a593Smuzhiyun 	int ret = 0;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	for ( i = 0; i < num_patterns; i++) {
211*4882a593Smuzhiyun 		move64(&(pattern[i]), pmem++);
212*4882a593Smuzhiyun 		/*
213*4882a593Smuzhiyun 		 * Put a different pattern on the data lines: otherwise they
214*4882a593Smuzhiyun 		 * may float long enough to read back what we wrote.
215*4882a593Smuzhiyun 		 */
216*4882a593Smuzhiyun 		move64(&otherpattern, pmem--);
217*4882a593Smuzhiyun 		move64(pmem, &temp64);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun #ifdef INJECT_DATA_ERRORS
220*4882a593Smuzhiyun 		temp64 ^= 0x00008000;
221*4882a593Smuzhiyun #endif
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 		if (temp64 != pattern[i]){
224*4882a593Smuzhiyun 			pathi = (pattern[i]>>32) & 0xffffffff;
225*4882a593Smuzhiyun 			patlo = pattern[i] & 0xffffffff;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 			hi = (temp64>>32) & 0xffffffff;
228*4882a593Smuzhiyun 			lo = temp64 & 0xffffffff;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 			post_log("Memory (data line) error at %08x, "
231*4882a593Smuzhiyun 				  "wrote %08x%08x, read %08x%08x !\n",
232*4882a593Smuzhiyun 					  pmem, pathi, patlo, hi, lo);
233*4882a593Smuzhiyun 			ret = -1;
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 	return ret;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
memory_post_addrline(ulong * testaddr,ulong * base,ulong size)239*4882a593Smuzhiyun static int memory_post_addrline(ulong *testaddr, ulong *base, ulong size)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	ulong *target;
242*4882a593Smuzhiyun 	ulong *end;
243*4882a593Smuzhiyun 	ulong readback;
244*4882a593Smuzhiyun 	ulong xor;
245*4882a593Smuzhiyun 	int   ret = 0;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	end = (ulong *)((ulong)base + size);	/* pointer arith! */
248*4882a593Smuzhiyun 	xor = 0;
249*4882a593Smuzhiyun 	for(xor = sizeof(ulong); xor > 0; xor <<= 1) {
250*4882a593Smuzhiyun 		target = (ulong *)((ulong)testaddr ^ xor);
251*4882a593Smuzhiyun 		if((target >= base) && (target < end)) {
252*4882a593Smuzhiyun 			*testaddr = ~*target;
253*4882a593Smuzhiyun 			readback  = *target;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun #ifdef INJECT_ADDRESS_ERRORS
256*4882a593Smuzhiyun 			if(xor == 0x00008000) {
257*4882a593Smuzhiyun 				readback = *testaddr;
258*4882a593Smuzhiyun 			}
259*4882a593Smuzhiyun #endif
260*4882a593Smuzhiyun 			if(readback == *testaddr) {
261*4882a593Smuzhiyun 				post_log("Memory (address line) error at %08x<->%08x, "
262*4882a593Smuzhiyun 					"XOR value %08x !\n",
263*4882a593Smuzhiyun 					testaddr, target, xor);
264*4882a593Smuzhiyun 				ret = -1;
265*4882a593Smuzhiyun 			}
266*4882a593Smuzhiyun 		}
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 	return ret;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
memory_post_test1(unsigned long start,unsigned long size,unsigned long val)271*4882a593Smuzhiyun static int memory_post_test1(unsigned long start,
272*4882a593Smuzhiyun 			      unsigned long size,
273*4882a593Smuzhiyun 			      unsigned long val)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	unsigned long i;
276*4882a593Smuzhiyun 	ulong *mem = (ulong *) start;
277*4882a593Smuzhiyun 	ulong readback;
278*4882a593Smuzhiyun 	int ret = 0;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong); i++) {
281*4882a593Smuzhiyun 		mem[i] = val;
282*4882a593Smuzhiyun 		if (i % 1024 == 0)
283*4882a593Smuzhiyun 			WATCHDOG_RESET();
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong) && !ret; i++) {
287*4882a593Smuzhiyun 		readback = mem[i];
288*4882a593Smuzhiyun 		if (readback != val) {
289*4882a593Smuzhiyun 			post_log("Memory error at %08x, "
290*4882a593Smuzhiyun 				  "wrote %08x, read %08x !\n",
291*4882a593Smuzhiyun 					  mem + i, val, readback);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 			ret = -1;
294*4882a593Smuzhiyun 			break;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 		if (i % 1024 == 0)
297*4882a593Smuzhiyun 			WATCHDOG_RESET();
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	return ret;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
memory_post_test2(unsigned long start,unsigned long size)303*4882a593Smuzhiyun static int memory_post_test2(unsigned long start, unsigned long size)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	unsigned long i;
306*4882a593Smuzhiyun 	ulong *mem = (ulong *) start;
307*4882a593Smuzhiyun 	ulong readback;
308*4882a593Smuzhiyun 	int ret = 0;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong); i++) {
311*4882a593Smuzhiyun 		mem[i] = 1 << (i % 32);
312*4882a593Smuzhiyun 		if (i % 1024 == 0)
313*4882a593Smuzhiyun 			WATCHDOG_RESET();
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong) && !ret; i++) {
317*4882a593Smuzhiyun 		readback = mem[i];
318*4882a593Smuzhiyun 		if (readback != (1 << (i % 32))) {
319*4882a593Smuzhiyun 			post_log("Memory error at %08x, "
320*4882a593Smuzhiyun 				  "wrote %08x, read %08x !\n",
321*4882a593Smuzhiyun 					  mem + i, 1 << (i % 32), readback);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 			ret = -1;
324*4882a593Smuzhiyun 			break;
325*4882a593Smuzhiyun 		}
326*4882a593Smuzhiyun 		if (i % 1024 == 0)
327*4882a593Smuzhiyun 			WATCHDOG_RESET();
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return ret;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
memory_post_test3(unsigned long start,unsigned long size)333*4882a593Smuzhiyun static int memory_post_test3(unsigned long start, unsigned long size)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	unsigned long i;
336*4882a593Smuzhiyun 	ulong *mem = (ulong *) start;
337*4882a593Smuzhiyun 	ulong readback;
338*4882a593Smuzhiyun 	int ret = 0;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong); i++) {
341*4882a593Smuzhiyun 		mem[i] = i;
342*4882a593Smuzhiyun 		if (i % 1024 == 0)
343*4882a593Smuzhiyun 			WATCHDOG_RESET();
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong) && !ret; i++) {
347*4882a593Smuzhiyun 		readback = mem[i];
348*4882a593Smuzhiyun 		if (readback != i) {
349*4882a593Smuzhiyun 			post_log("Memory error at %08x, "
350*4882a593Smuzhiyun 				  "wrote %08x, read %08x !\n",
351*4882a593Smuzhiyun 					  mem + i, i, readback);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 			ret = -1;
354*4882a593Smuzhiyun 			break;
355*4882a593Smuzhiyun 		}
356*4882a593Smuzhiyun 		if (i % 1024 == 0)
357*4882a593Smuzhiyun 			WATCHDOG_RESET();
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return ret;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
memory_post_test4(unsigned long start,unsigned long size)363*4882a593Smuzhiyun static int memory_post_test4(unsigned long start, unsigned long size)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	unsigned long i;
366*4882a593Smuzhiyun 	ulong *mem = (ulong *) start;
367*4882a593Smuzhiyun 	ulong readback;
368*4882a593Smuzhiyun 	int ret = 0;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong); i++) {
371*4882a593Smuzhiyun 		mem[i] = ~i;
372*4882a593Smuzhiyun 		if (i % 1024 == 0)
373*4882a593Smuzhiyun 			WATCHDOG_RESET();
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	for (i = 0; i < size / sizeof (ulong) && !ret; i++) {
377*4882a593Smuzhiyun 		readback = mem[i];
378*4882a593Smuzhiyun 		if (readback != ~i) {
379*4882a593Smuzhiyun 			post_log("Memory error at %08x, "
380*4882a593Smuzhiyun 				  "wrote %08x, read %08x !\n",
381*4882a593Smuzhiyun 					  mem + i, ~i, readback);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 			ret = -1;
384*4882a593Smuzhiyun 			break;
385*4882a593Smuzhiyun 		}
386*4882a593Smuzhiyun 		if (i % 1024 == 0)
387*4882a593Smuzhiyun 			WATCHDOG_RESET();
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	return ret;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
memory_post_test_lines(unsigned long start,unsigned long size)393*4882a593Smuzhiyun static int memory_post_test_lines(unsigned long start, unsigned long size)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	int ret = 0;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	ret = memory_post_dataline((unsigned long long *)start);
398*4882a593Smuzhiyun 	WATCHDOG_RESET();
399*4882a593Smuzhiyun 	if (!ret)
400*4882a593Smuzhiyun 		ret = memory_post_addrline((ulong *)start, (ulong *)start,
401*4882a593Smuzhiyun 				size);
402*4882a593Smuzhiyun 	WATCHDOG_RESET();
403*4882a593Smuzhiyun 	if (!ret)
404*4882a593Smuzhiyun 		ret = memory_post_addrline((ulong *)(start+size-8),
405*4882a593Smuzhiyun 				(ulong *)start, size);
406*4882a593Smuzhiyun 	WATCHDOG_RESET();
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	return ret;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
memory_post_test_patterns(unsigned long start,unsigned long size)411*4882a593Smuzhiyun static int memory_post_test_patterns(unsigned long start, unsigned long size)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	int ret = 0;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	ret = memory_post_test1(start, size, 0x00000000);
416*4882a593Smuzhiyun 	WATCHDOG_RESET();
417*4882a593Smuzhiyun 	if (!ret)
418*4882a593Smuzhiyun 		ret = memory_post_test1(start, size, 0xffffffff);
419*4882a593Smuzhiyun 	WATCHDOG_RESET();
420*4882a593Smuzhiyun 	if (!ret)
421*4882a593Smuzhiyun 		ret = memory_post_test1(start, size, 0x55555555);
422*4882a593Smuzhiyun 	WATCHDOG_RESET();
423*4882a593Smuzhiyun 	if (!ret)
424*4882a593Smuzhiyun 		ret = memory_post_test1(start, size, 0xaaaaaaaa);
425*4882a593Smuzhiyun 	WATCHDOG_RESET();
426*4882a593Smuzhiyun 	if (!ret)
427*4882a593Smuzhiyun 		ret = memory_post_test2(start, size);
428*4882a593Smuzhiyun 	WATCHDOG_RESET();
429*4882a593Smuzhiyun 	if (!ret)
430*4882a593Smuzhiyun 		ret = memory_post_test3(start, size);
431*4882a593Smuzhiyun 	WATCHDOG_RESET();
432*4882a593Smuzhiyun 	if (!ret)
433*4882a593Smuzhiyun 		ret = memory_post_test4(start, size);
434*4882a593Smuzhiyun 	WATCHDOG_RESET();
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	return ret;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
memory_post_test_regions(unsigned long start,unsigned long size)439*4882a593Smuzhiyun static int memory_post_test_regions(unsigned long start, unsigned long size)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	unsigned long i;
442*4882a593Smuzhiyun 	int ret = 0;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	for (i = 0; i < (size >> 20) && (!ret); i++) {
445*4882a593Smuzhiyun 		if (!ret)
446*4882a593Smuzhiyun 			ret = memory_post_test_patterns(start + (i << 20),
447*4882a593Smuzhiyun 				0x800);
448*4882a593Smuzhiyun 		if (!ret)
449*4882a593Smuzhiyun 			ret = memory_post_test_patterns(start + (i << 20) +
450*4882a593Smuzhiyun 				0xff800, 0x800);
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	return ret;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
memory_post_tests(unsigned long start,unsigned long size)456*4882a593Smuzhiyun static int memory_post_tests(unsigned long start, unsigned long size)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	int ret = 0;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	ret = memory_post_test_lines(start, size);
461*4882a593Smuzhiyun 	if (!ret)
462*4882a593Smuzhiyun 		ret = memory_post_test_patterns(start, size);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	return ret;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun  * !! this is only valid, if you have contiguous memory banks !!
469*4882a593Smuzhiyun  */
470*4882a593Smuzhiyun __attribute__((weak))
arch_memory_test_prepare(u32 * vstart,u32 * size,phys_addr_t * phys_offset)471*4882a593Smuzhiyun int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	bd_t *bd = gd->bd;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	*vstart = CONFIG_SYS_SDRAM_BASE;
476*4882a593Smuzhiyun 	*size = (gd->ram_size >= 256 << 20 ?
477*4882a593Smuzhiyun 			256 << 20 : gd->ram_size) - (1 << 20);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	/* Limit area to be tested with the board info struct */
480*4882a593Smuzhiyun 	if ((*vstart) + (*size) > (ulong)bd)
481*4882a593Smuzhiyun 		*size = (ulong)bd - *vstart;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	return 0;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun __attribute__((weak))
arch_memory_test_advance(u32 * vstart,u32 * size,phys_addr_t * phys_offset)487*4882a593Smuzhiyun int arch_memory_test_advance(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	return 1;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun __attribute__((weak))
arch_memory_test_cleanup(u32 * vstart,u32 * size,phys_addr_t * phys_offset)493*4882a593Smuzhiyun int arch_memory_test_cleanup(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	return 0;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun __attribute__((weak))
arch_memory_failure_handle(void)499*4882a593Smuzhiyun void arch_memory_failure_handle(void)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	return;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
memory_regions_post_test(int flags)504*4882a593Smuzhiyun int memory_regions_post_test(int flags)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	int ret = 0;
507*4882a593Smuzhiyun 	phys_addr_t phys_offset = 0;
508*4882a593Smuzhiyun 	u32 memsize, vstart;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	arch_memory_test_prepare(&vstart, &memsize, &phys_offset);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	ret = memory_post_test_lines(vstart, memsize);
513*4882a593Smuzhiyun 	if (!ret)
514*4882a593Smuzhiyun 		ret = memory_post_test_regions(vstart, memsize);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	return ret;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
memory_post_test(int flags)519*4882a593Smuzhiyun int memory_post_test(int flags)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun 	int ret = 0;
522*4882a593Smuzhiyun 	phys_addr_t phys_offset = 0;
523*4882a593Smuzhiyun 	u32 memsize, vstart;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	arch_memory_test_prepare(&vstart, &memsize, &phys_offset);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	do {
528*4882a593Smuzhiyun 		if (flags & POST_SLOWTEST) {
529*4882a593Smuzhiyun 			ret = memory_post_tests(vstart, memsize);
530*4882a593Smuzhiyun 		} else {			/* POST_NORMAL */
531*4882a593Smuzhiyun 			ret = memory_post_test_regions(vstart, memsize);
532*4882a593Smuzhiyun 		}
533*4882a593Smuzhiyun 	} while (!ret &&
534*4882a593Smuzhiyun 		!arch_memory_test_advance(&vstart, &memsize, &phys_offset));
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	arch_memory_test_cleanup(&vstart, &memsize, &phys_offset);
537*4882a593Smuzhiyun 	if (ret)
538*4882a593Smuzhiyun 		arch_memory_failure_handle();
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	return ret;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun #endif /* CONFIG_POST&(CONFIG_SYS_POST_MEMORY|CONFIG_SYS_POST_MEM_REGIONS) */
544