blob: 7638a99f2ebf67469511e7d06c2d9f9898769eb0 [file] [log] [blame]
Mike Frysinger813531f2011-05-10 13:35:40 +00001/*
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
Tom Rini3dd5d4a2022-12-04 10:14:17 -05009#if CFG_POST & CFG_SYS_POST_FLASH
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <config.h>
Mike Frysinger813531f2011-05-10 13:35:40 +000011#include <malloc.h>
12#include <post.h>
13#include <flash.h>
14
Mike Frysinger813531f2011-05-10 13:35:40 +000015/*
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
Tom Rini3dd5d4a2022-12-04 10:14:17 -050025#ifndef CFG_SYS_POST_FLASH_NUM
26# define CFG_SYS_POST_FLASH_NUM 0
Mike Frysinger813531f2011-05-10 13:35:40 +000027#endif
Tom Rini3dd5d4a2022-12-04 10:14:17 -050028#if CFG_SYS_POST_FLASH_START >= CFG_SYS_POST_FLASH_END
Mike Frysinger813531f2011-05-10 13:35:40 +000029# error "invalid flash block start/end"
30#endif
31
Mike Frysinger813531f2011-05-10 13:35:40 +000032static 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
49int 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;
Tom Rini3dd5d4a2022-12-04 10:14:17 -050061 info = &flash_info[CFG_SYS_POST_FLASH_NUM];
62 n_start = CFG_SYS_POST_FLASH_START;
63 n_end = CFG_SYS_POST_FLASH_END;
Mike Frysinger813531f2011-05-10 13:35:40 +000064
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