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