1 /*
2  * Parallel NOR Flash tests
3  *
4  * Copyright (c) 2005-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8 
9 #if CFG_POST & CFG_SYS_POST_FLASH
10 #include <common.h>
11 #include <malloc.h>
12 #include <post.h>
13 #include <flash.h>
14 
15 
16 /*
17  * This code will walk over the declared sectors erasing them,
18  * then programming them, then verifying the written contents.
19  * Possible future work:
20  *  - verify sectors before/after are not erased/written
21  *  - verify partial writes (e.g. programming only middle of sector)
22  *  - verify the contents of the erased sector
23  *  - better seed pattern than 0x00..0xff
24  */
25 
26 #ifndef CFG_SYS_POST_FLASH_NUM
27 # define CFG_SYS_POST_FLASH_NUM 0
28 #endif
29 #if CFG_SYS_POST_FLASH_START >= CFG_SYS_POST_FLASH_END
30 # error "invalid flash block start/end"
31 #endif
32 
seed_src_data(void * ptr,ulong * old_len,ulong new_len)33 static void *seed_src_data(void *ptr, ulong *old_len, ulong new_len)
34 {
35 	unsigned char *p;
36 	ulong i;
37 
38 	p = ptr = realloc(ptr, new_len);
39 	if (!ptr)
40 		return ptr;
41 
42 	for (i = *old_len; i < new_len; ++i)
43 		p[i] = i;
44 
45 	*old_len = new_len;
46 
47 	return ptr;
48 }
49 
flash_post_test(int flags)50 int flash_post_test(int flags)
51 {
52 	ulong len;
53 	void *src;
54 	int ret, n, n_start, n_end;
55 	flash_info_t *info;
56 
57 	/* the output from the common flash layers needs help */
58 	puts("\n");
59 
60 	len = 0;
61 	src = NULL;
62 	info = &flash_info[CFG_SYS_POST_FLASH_NUM];
63 	n_start = CFG_SYS_POST_FLASH_START;
64 	n_end = CFG_SYS_POST_FLASH_END;
65 
66 	for (n = n_start; n < n_end; ++n) {
67 		ulong s_start, s_len, s_off;
68 
69 		s_start = info->start[n];
70 		s_len = flash_sector_size(info, n);
71 		s_off = s_start - info->start[0];
72 
73 		src = seed_src_data(src, &len, s_len);
74 		if (!src) {
75 			printf("malloc(%#lx) failed\n", s_len);
76 			return 1;
77 		}
78 
79 		printf("\tsector %i: %#lx +%#lx", n, s_start, s_len);
80 
81 		ret = flash_erase(info, n, n + 1);
82 		if (ret) {
83 			flash_perror(ret);
84 			break;
85 		}
86 
87 		ret = write_buff(info, src, s_start, s_len);
88 		if (ret) {
89 			flash_perror(ret);
90 			break;
91 		}
92 
93 		ret = memcmp(src, (void *)s_start, s_len);
94 		if (ret) {
95 			printf(" verify failed with %i\n", ret);
96 			break;
97 		}
98 	}
99 
100 	free(src);
101 
102 	return ret;
103 }
104 
105 #endif
106