summaryrefslogtreecommitdiff
path: root/board/mpl/common/memtst.c
blob: c994a8b05f4e48ebbdf67fc6f8160904c7f52e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

/* NOT Used yet...
  add following code to PIP405.c :
int testdram (void)
{
	unsigned char s[32];
	int i;

	i = getenv_r ("testmem", s, 32);
	if (i != 0) {
		i = (int) simple_strtoul (s, NULL, 10);
		if ((i > 0) && (i < 0xf)) {
			printf ("testing ");
			i = mem_test (0, ramsize, i);
			if (i > 0)
				printf ("ERROR ");
			else
				printf ("Ok ");
		}
	}
	return (1);
}
*/


#include <common.h>
#include <asm/processor.h>
#include <405gp_i2c.h>

#define FALSE           0
#define TRUE            1

#define TEST_QUIET 			8
#define TEST_SHOW_PROG 	4
#define TEST_SHOW_ERR 	2
#define TEST_SHOW_ALL		1

#define TESTPAT1 0xAA55AA55
#define TESTPAT2 0x55AA55AA
#define TEST_PASSED 0
#define TEST_FAILED 1
#define MEGABYTE (1024*1024)


typedef struct {
	volatile unsigned long pat1;
	volatile unsigned long pat2;
} RAM_MEMTEST_PATTERN2;

typedef struct {
	volatile unsigned long addr;
} RAM_MEMTEST_ADDRLINE;

static __inline unsigned long Swap_32 (unsigned long val)
{
	return (((val << 16) & 0xFFFF0000) | ((val >> 16) & 0x0000FFFF));
}

void testm_puts (int quiet, char *buf)
{
	if ((quiet & TEST_SHOW_ALL) == TEST_SHOW_ALL)
		puts (buf);
}


void Write_Error (int mode, unsigned long addr, unsigned long expected,
				  unsigned long actual)
{

	char dispbuf[64];

	sprintf (dispbuf, "\n ERROR @ 0x%08lX: (exp: 0x%08lX act: 0x%08lX) ",
			 addr, expected, actual);
	testm_puts (((mode & TEST_SHOW_ERR) ==
				 TEST_SHOW_ERR) ? TEST_SHOW_ALL : mode, dispbuf);
}


/*
 * fills the memblock of <size> bytes from <startaddr> with pat1 and pat2
 */


void RAM_MemTest_WritePattern2 (unsigned long startaddr,
								unsigned long size, unsigned long pat1,
								unsigned long pat2)
{
	RAM_MEMTEST_PATTERN2 *p, *pe;

	p = (RAM_MEMTEST_PATTERN2 *) startaddr;
	pe = (RAM_MEMTEST_PATTERN2 *) (startaddr + size);

	while (p < pe) {
		p->pat1 = pat1;
		p->pat2 = pat2;
		p++;
	}							/* endwhile */
}

/*
 * checks the memblock of <size> bytes from <startaddr> with pat1 and pat2
 * returns the address of the first error or NULL if all is well
 */

void *RAM_MemTest_CheckPattern2 (int mode, unsigned long startaddr,
								 unsigned long size, unsigned long pat1,
								 unsigned long pat2)
{
	RAM_MEMTEST_PATTERN2 *p, *pe;
	unsigned long actual1, actual2;

	p = (RAM_MEMTEST_PATTERN2 *) startaddr;
	pe = (RAM_MEMTEST_PATTERN2 *) (startaddr + size);

	while (p < pe) {
		actual1 = p->pat1;
		actual2 = p->pat2;

		if (actual1 != pat1) {
			Write_Error (mode, (unsigned long) &(p->pat1), pat1, actual1);
			return ((void *) &(p->pat1));
		}
		/* endif */
		if (actual2 != pat2) {
			Write_Error (mode, (unsigned long) &(p->pat2), pat2, actual2);
			return ((void *) &(p->pat2));
		}
		/* endif */
		p++;
	}							/* endwhile */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with the address
 */

void RAM_MemTest_WriteAddrLine (unsigned long startaddr,
								unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;

	p = (RAM_MEMTEST_ADDRLINE *) startaddr;
	pe = (RAM_MEMTEST_ADDRLINE *) (startaddr + size);

	if (!swapped) {
		while (p < pe) {
			p->addr = (unsigned long) p;
			p++;
		}						/* endwhile */
	} else {
		while (p < pe) {
			p->addr = Swap_32 ((unsigned long) p);
			p++;
		}						/* endwhile */
	}							/* endif */
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckAddrLine (int mode, unsigned long startaddr,
								 unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;
	unsigned long actual, expected;

	p = (RAM_MEMTEST_ADDRLINE *) startaddr;
	pe = (RAM_MEMTEST_ADDRLINE *) (startaddr + size);

	if (!swapped) {
		while (p < pe) {
			actual = p->addr;
			expected = (unsigned long) p;
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p++;
		}						/* endwhile */
	} else {
		while (p < pe) {
			actual = p->addr;
			expected = Swap_32 ((unsigned long) p);
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p++;
		}						/* endwhile */
	}							/* endif */

	return (NULL);
}

/*
 * checks the memblock of <size> bytes from <startaddr+size>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckAddrLineReverse (int mode, unsigned long startaddr,
										unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;
	unsigned long actual, expected;

	p = (RAM_MEMTEST_ADDRLINE *) (startaddr + size - sizeof (p->addr));
	pe = (RAM_MEMTEST_ADDRLINE *) startaddr;

	if (!swapped) {
		while (p > pe) {
			actual = p->addr;
			expected = (unsigned long) p;
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p--;
		}						/* endwhile */
	} else {
		while (p > pe) {
			actual = p->addr;
			expected = Swap_32 ((unsigned long) p);
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p--;
		}						/* endwhile */
	}							/* endif */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with walking bit pattern
 */

void RAM_MemTest_WriteWalkBit (unsigned long startaddr, unsigned long size)
{
	volatile unsigned long *p, *pe;
	unsigned long i;

	p = (unsigned long *) startaddr;
	pe = (unsigned long *) (startaddr + size);
	i = 0;

	while (p < pe) {
		*p = 1UL << i;
		i = (i + 1 + (((unsigned long) p) >> 7)) % 32;
		p++;
	}							/* endwhile */
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckWalkBit (int mode, unsigned long startaddr,
								unsigned long size)
{
	volatile unsigned long *p, *pe;
	unsigned long actual, expected;
	unsigned long i;

	p = (unsigned long *) startaddr;
	pe = (unsigned long *) (startaddr + size);
	i = 0;

	while (p < pe) {
		actual = *p;
		expected = (1UL << i);
		if (actual != expected) {
			Write_Error (mode, (unsigned long) p, expected, actual);
			return ((void *) p);
		}						/* endif */
		i = (i + 1 + (((unsigned long) p) >> 7)) % 32;
		p++;
	}							/* endwhile */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with "random" pattern
 */

void RAM_MemTest_WriteRandomPattern (unsigned long startaddr,
									 unsigned long size,
									 unsigned long *pat)
{
	unsigned long i, p;

	p = *pat;

	for (i = 0; i < (size / 4); i++) {
		*(unsigned long *) (startaddr + i * 4) = p;
		if ((p % 2) > 0) {
			p ^= i;
			p >>= 1;
			p |= 0x80000000;
		} else {
			p ^= ~i;
			p >>= 1;
		}						/* endif */
	}							/* endfor */
	*pat = p;
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckRandomPattern (int mode, unsigned long startaddr,
									  unsigned long size,
									  unsigned long *pat)
{
	void *perr = NULL;
	unsigned long i, p, p1;

	p = *pat;

	for (i = 0; i < (size / 4); i++) {
		p1 = *(unsigned long *) (startaddr + i * 4);
		if (p1 != p) {
			if (perr == NULL) {
				Write_Error (mode, startaddr + i * 4, p, p1);
				perr = (void *) (startaddr + i * 4);
			}					/* endif */
		}
		/* endif */
		if ((p % 2) > 0) {
			p ^= i;
			p >>= 1;
			p |= 0x80000000;
		} else {
			p ^= ~i;
			p >>= 1;
		}						/* endif */
	}							/* endfor */

	*pat = p;
	return (perr);
}


void RAM_MemTest_WriteData1 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WritePattern2 (startaddr, size, TESTPAT1, TESTPAT2);
}

void *RAM_MemTest_CheckData1 (int mode, unsigned long startaddr,
							  unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckPattern2
			(mode, startaddr, size, TESTPAT1, TESTPAT2));
}

void RAM_MemTest_WriteData2 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WritePattern2 (startaddr, size, TESTPAT2, TESTPAT1);
}

void *RAM_MemTest_CheckData2 (int mode, unsigned long startaddr,
							  unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckPattern2
			(mode, startaddr, size, TESTPAT2, TESTPAT1));
}

void RAM_MemTest_WriteAddr1 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WriteAddrLine (startaddr, size, FALSE);
}

void *RAM_MemTest_Check1Addr1 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLine (mode, startaddr, size, FALSE));
}

void *RAM_MemTest_Check2Addr1 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLineReverse
			(mode, startaddr, size, FALSE));
}

void RAM_MemTest_WriteAddr2 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WriteAddrLine (startaddr, size, TRUE);
}

void *RAM_MemTest_Check1Addr2 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLine (mode, startaddr, size, TRUE));
}

void *RAM_MemTest_Check2Addr2 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLineReverse
			(mode, startaddr, size, TRUE));
}


typedef struct {
	void (*test_write) (unsigned long startaddr, unsigned long size,
						unsigned long *pat);
	char *test_write_desc;
	void *(*test_check1) (int mode, unsigned long startaddr,
						  unsigned long size, unsigned long *pat);
	void *(*test_check2) (int mode, unsigned long startaddr,
						  unsigned long size, unsigned long *pat);
} RAM_MEMTEST_FUNC;


#define TEST_STAGES 5
static RAM_MEMTEST_FUNC test_stage[TEST_STAGES] = {
	{RAM_MemTest_WriteData1, "data test 1...\n", RAM_MemTest_CheckData1,
	 NULL},
	{RAM_MemTest_WriteData2, "data test 2...\n", RAM_MemTest_CheckData2,
	 NULL},
	{RAM_MemTest_WriteAddr1, "address line test...\n",
	 RAM_MemTest_Check1Addr1, RAM_MemTest_Check2Addr1},
	{RAM_MemTest_WriteAddr2, "address line test (swapped)...\n",
	 RAM_MemTest_Check1Addr2, RAM_MemTest_Check2Addr2},
	{RAM_MemTest_WriteRandomPattern, "random data test...\n",
	 RAM_MemTest_CheckRandomPattern, NULL}
};

void mem_test_reloc(void)
{
	DECLARE_GLOBAL_DATA_PTR;
	unsigned long addr;
	int i;
	for (i=0; i< TEST_STAGES; i++) {
 		addr = (ulong) (test_stage[i].test_write) + gd->reloc_off;
		test_stage[i].test_write=
			(void (*) (unsigned long startaddr, unsigned long size,
						unsigned long *pat))addr;
 		addr = (ulong) (test_stage[i].test_write_desc) + gd->reloc_off;
		test_stage[i].test_write_desc=(char *)addr;
 		if(test_stage[i].test_check1) {
			addr = (ulong) (test_stage[i].test_check1) + gd->reloc_off;
			test_stage[i].test_check1=
				(void *(*) (int mode, unsigned long startaddr,
				 unsigned long size, unsigned long *pat))addr;
		}
 		if(test_stage[i].test_check2) {
			addr = (ulong) (test_stage[i].test_check2) + gd->reloc_off;
			test_stage[i].test_check2=
				(void *(*) (int mode, unsigned long startaddr,
				 unsigned long size, unsigned long *pat))addr;
		}
	}
}


int mem_test (unsigned long start, unsigned long ramsize, int quiet, int reloc)
{
	unsigned long errors, stage;
	unsigned long startaddr, size, i;
	const unsigned long blocksize = 0x80000;	/* check in 512KB blocks */
	unsigned long *perr;
	unsigned long rdatapat;
	char dispbuf[80];
	int status = TEST_PASSED;
	int prog = 0;

	errors = 0;
	startaddr = start;
	size = ramsize;
	if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
		prog++;
		printf (".");
	}
	sprintf (dispbuf, "\nMemory Test: addr = 0x%lx size = 0x%lx\n",
			 startaddr, size);
	testm_puts (quiet, dispbuf);
	for (stage = 0; stage < TEST_STAGES; stage++) {
		sprintf (dispbuf, test_stage[stage].test_write_desc);
		testm_puts (quiet, dispbuf);
		/* fill SDRAM */
		rdatapat = 0x12345678;
		sprintf (dispbuf, "writing block:     ");
		testm_puts (quiet, dispbuf);
		for (i = 0; i < size; i += blocksize) {
			sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
			testm_puts (quiet, dispbuf);
			test_stage[stage].test_write (startaddr + i, blocksize,
										  &rdatapat);
		}						/* endfor */
		sprintf (dispbuf, "\n");
		testm_puts (quiet, dispbuf);
		if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
			prog++;
			printf (".");
		}
		/* check SDRAM */
		rdatapat = 0x12345678;
		sprintf (dispbuf, "checking block:     ");
		testm_puts (quiet, dispbuf);
		for (i = 0; i < size; i += blocksize) {
			sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
			testm_puts (quiet, dispbuf);
			if ((perr =
				 test_stage[stage].test_check1 (quiet, startaddr + i,
												blocksize,
												&rdatapat)) != NULL) {
				status = TEST_FAILED;
			}					/* endif */
		}						/* endfor */
		sprintf (dispbuf, "\n");
		testm_puts (quiet, dispbuf);
		if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
			prog++;
			printf (".");
		}
		if (test_stage[stage].test_check2 != NULL) {
			/* check2 SDRAM */
			sprintf (dispbuf, "2nd checking block:     ");
			rdatapat = 0x12345678;
			testm_puts (quiet, dispbuf);
			for (i = 0; i < size; i += blocksize) {
				sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
				testm_puts (quiet, dispbuf);
				if ((perr =
					 test_stage[stage].test_check2 (quiet, startaddr + i,
													blocksize,
													&rdatapat)) != NULL) {
					status = TEST_FAILED;
				}				/* endif */
			}					/* endfor */
			sprintf (dispbuf, "\n");
			testm_puts (quiet, dispbuf);
			if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
				prog++;
				printf (".");
			}
		}

	}							/* next stage */
	if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
		while (prog-- > 0)
			printf ("\b \b");
	}

	if (status == TEST_FAILED)
		errors++;

	return (errors);
}