1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// flashdev.c
|
4 |
|
|
//
|
5 |
|
|
// Simple tests for FLASHdev driver
|
6 |
|
|
//
|
7 |
|
|
//=================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//=================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): nickg, gthomas
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2005-10-25
|
45 |
|
|
// Description: Simple test of FLASH I/O device. This is really
|
46 |
|
|
// just a copy of the flash1 test, but using the
|
47 |
|
|
// /dev/flash interface instead of the flash library.
|
48 |
|
|
//
|
49 |
|
|
// Options:
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//=================================================================
|
52 |
|
|
|
53 |
|
|
// #define DEBUG_PRINTFS
|
54 |
|
|
|
55 |
|
|
#include <pkgconf/hal.h>
|
56 |
|
|
#include <pkgconf/io_flash.h>
|
57 |
|
|
#include CYGHWR_MEMORY_LAYOUT_H
|
58 |
|
|
|
59 |
|
|
#include <cyg/infra/diag.h>
|
60 |
|
|
#include <cyg/infra/testcase.h>
|
61 |
|
|
|
62 |
|
|
#include <cyg/io/io.h>
|
63 |
|
|
#include <cyg/io/config_keys.h>
|
64 |
|
|
#include <cyg/io/flash.h>
|
65 |
|
|
#include <errno.h>
|
66 |
|
|
#include <string.h>
|
67 |
|
|
#include <cyg/hal/hal_if.h>
|
68 |
|
|
|
69 |
|
|
//=================================================================
|
70 |
|
|
// Config options.
|
71 |
|
|
|
72 |
|
|
#define FLASH_TEST_OFFSET 0
|
73 |
|
|
#define FLASH_TEST_LENGTH 0x40000
|
74 |
|
|
|
75 |
|
|
//=================================================================
|
76 |
|
|
// A FIS entry named "flashtest" of at least FLASH_TEST_LENGTH bytes
|
77 |
|
|
// must exist for this test to work. It can be created with the
|
78 |
|
|
// following command:
|
79 |
|
|
//
|
80 |
|
|
// RedBoot> fis cre -b %{freememlo} -l 0x100000 flashtest
|
81 |
|
|
|
82 |
|
|
#define FLASH_TEST_DEVICE "/dev/flash/fis/flashtest"
|
83 |
|
|
|
84 |
|
|
// If it does not exist, a FIS entry named "jffs2test" will be used
|
85 |
|
|
// if present, as this may have been set up for jffs2 testing.
|
86 |
|
|
#define FLASH_TEST_DEVICE2 "/dev/flash/fis/jffs2test"
|
87 |
|
|
|
88 |
|
|
//=================================================================
|
89 |
|
|
|
90 |
|
|
#if !defined(CYGPKG_IO_FLASH_BLOCK_DEVICE)
|
91 |
|
|
externC void
|
92 |
|
|
cyg_start( void )
|
93 |
|
|
{
|
94 |
|
|
CYG_TEST_INIT();
|
95 |
|
|
CYG_TEST_NA("Only usable with flash block device driver");
|
96 |
|
|
}
|
97 |
|
|
#else
|
98 |
|
|
|
99 |
|
|
//=================================================================
|
100 |
|
|
|
101 |
|
|
externC void
|
102 |
|
|
cyg_start( void )
|
103 |
|
|
{
|
104 |
|
|
Cyg_ErrNo stat;
|
105 |
|
|
cyg_io_handle_t flash_handle;
|
106 |
|
|
cyg_io_flash_getconfig_erase_t e;
|
107 |
|
|
cyg_io_flash_getconfig_devsize_t d;
|
108 |
|
|
cyg_io_flash_getconfig_blocksize_t b;
|
109 |
|
|
#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
|
110 |
|
|
cyg_io_flash_getconfig_lock_t l;
|
111 |
|
|
cyg_io_flash_getconfig_unlock_t u;
|
112 |
|
|
#endif
|
113 |
|
|
CYG_ADDRWORD flash_start, flash_end;
|
114 |
|
|
CYG_ADDRWORD flash_test_start, flash_addr;
|
115 |
|
|
cyg_uint32 flash_block_size, flash_num_blocks;
|
116 |
|
|
CYG_ADDRWORD test_buf1, test_buf2;
|
117 |
|
|
cyg_uint32 *lp1, *lp2;
|
118 |
|
|
int i;
|
119 |
|
|
unsigned len;
|
120 |
|
|
cyg_bool passed, ok;
|
121 |
|
|
|
122 |
|
|
CYG_TEST_INIT();
|
123 |
|
|
|
124 |
|
|
passed = true;
|
125 |
|
|
|
126 |
|
|
if ((stat = cyg_io_lookup(FLASH_TEST_DEVICE, &flash_handle)) == -ENOENT) {
|
127 |
|
|
stat = cyg_io_lookup(FLASH_TEST_DEVICE2, &flash_handle);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
if (stat != 0) {
|
131 |
|
|
diag_printf("FLASH: driver init failed: %s\n", strerror(-stat));
|
132 |
|
|
CYG_TEST_FAIL_FINISH("FLASH driver init failed");
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
len = sizeof(d);
|
136 |
|
|
stat = cyg_io_get_config( flash_handle, CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &d, &len );
|
137 |
|
|
flash_start = 0;
|
138 |
|
|
// Keep 'end' address as last valid location, to avoid wrap around problems
|
139 |
|
|
flash_end = d.dev_size - 1;
|
140 |
|
|
|
141 |
|
|
len = sizeof(b);
|
142 |
|
|
b.offset = 0;
|
143 |
|
|
stat = cyg_io_get_config( flash_handle, CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE, &b, &len );
|
144 |
|
|
flash_block_size = b.block_size;
|
145 |
|
|
flash_num_blocks = d.dev_size/flash_block_size;
|
146 |
|
|
|
147 |
|
|
diag_printf("FLASH: %p - %p, %d blocks of 0x%x bytes each.\n",
|
148 |
|
|
(void*)flash_start, (void*)(flash_end + 1), flash_num_blocks, flash_block_size);
|
149 |
|
|
|
150 |
|
|
// Verify that the testing limits are within the bounds of the
|
151 |
|
|
// physical device. Also verify that the size matches with
|
152 |
|
|
// the erase block size on the device
|
153 |
|
|
if ((FLASH_TEST_OFFSET > (flash_end - flash_start)) ||
|
154 |
|
|
((FLASH_TEST_OFFSET + FLASH_TEST_LENGTH) > (flash_end - flash_start))) {
|
155 |
|
|
CYG_TEST_FAIL_FINISH("FLASH test region outside physical limits");
|
156 |
|
|
}
|
157 |
|
|
if ((FLASH_TEST_LENGTH % flash_block_size) != 0) {
|
158 |
|
|
CYG_TEST_FAIL_FINISH("FLASH test region must be integral multiple of erase block size");
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
// Allocate two buffers large enough for the test
|
162 |
|
|
test_buf1 = (CYG_ADDRWORD)CYGMEM_SECTION_heap1;
|
163 |
|
|
test_buf2 = test_buf1 + FLASH_TEST_LENGTH;
|
164 |
|
|
if (CYGMEM_SECTION_heap1_SIZE < (FLASH_TEST_LENGTH * 2)) {
|
165 |
|
|
CYG_TEST_FAIL_FINISH("FLASH not enough heap space - reduce size of test region");
|
166 |
|
|
}
|
167 |
|
|
diag_printf("... Using test buffers at %p and %p\n", (void *)test_buf1, (void *)test_buf2);
|
168 |
|
|
flash_test_start = flash_start + FLASH_TEST_OFFSET;
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
|
172 |
|
|
// Unlock test
|
173 |
|
|
diag_printf("... Unlock test\n");
|
174 |
|
|
ok = true;
|
175 |
|
|
u.offset = flash_test_start;
|
176 |
|
|
u.len = FLASH_TEST_LENGTH;
|
177 |
|
|
len = sizeof(u);
|
178 |
|
|
if ((stat = cyg_io_get_config(flash_handle, CYG_IO_GET_CONFIG_FLASH_UNLOCK, &u, &len ) ) != 0 || u.flasherr != 0)
|
179 |
|
|
{
|
180 |
|
|
diag_printf("FLASH: unlock failed: %s %s\n", strerror(stat), cyg_flash_errmsg(u.flasherr));
|
181 |
|
|
ok = false;
|
182 |
|
|
}
|
183 |
|
|
#endif
|
184 |
|
|
|
185 |
|
|
// Erase test
|
186 |
|
|
diag_printf("... Erase test\n");
|
187 |
|
|
ok = true;
|
188 |
|
|
|
189 |
|
|
e.offset = flash_test_start;
|
190 |
|
|
e.len = FLASH_TEST_LENGTH;
|
191 |
|
|
len = sizeof(e);
|
192 |
|
|
if ((stat = cyg_io_get_config(flash_handle, CYG_IO_GET_CONFIG_FLASH_ERASE, &e, &len ) ) != 0 || e.flasherr != 0)
|
193 |
|
|
{
|
194 |
|
|
diag_printf("FLASH: erase failed: %s %s\n", cyg_flash_errmsg(stat),cyg_flash_errmsg(e.flasherr));
|
195 |
|
|
ok = false;
|
196 |
|
|
}
|
197 |
|
|
len = FLASH_TEST_LENGTH;
|
198 |
|
|
if (ok && (stat = cyg_io_bread(flash_handle, (void *)test_buf1, &len, flash_test_start)) != 0)
|
199 |
|
|
{
|
200 |
|
|
diag_printf("FLASH: read/verify after erase failed: %s\n", cyg_flash_errmsg(stat));
|
201 |
|
|
ok = false;
|
202 |
|
|
}
|
203 |
|
|
lp1 = (cyg_uint32 *)test_buf1;
|
204 |
|
|
for (i = 0; i < FLASH_TEST_LENGTH; i += sizeof(cyg_uint32)) {
|
205 |
|
|
if (*lp1++ != 0xFFFFFFFF) {
|
206 |
|
|
diag_printf("FLASH: non-erased data found at offset %p\n", (void*)((CYG_ADDRWORD)(lp1-1) - test_buf1));
|
207 |
|
|
diag_dump_buf((void *)(lp1-1), 32);
|
208 |
|
|
ok = false;
|
209 |
|
|
break;
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
// Try reading in little pieces
|
214 |
|
|
len = FLASH_TEST_LENGTH;
|
215 |
|
|
flash_addr = flash_test_start;
|
216 |
|
|
while (len > 0) {
|
217 |
|
|
cyg_uint32 l = 0x200;
|
218 |
|
|
if ((stat = cyg_io_bread(flash_handle, (void *)test_buf1, &l, flash_addr)) != CYG_FLASH_ERR_OK) {
|
219 |
|
|
diag_printf("FLASH: read[short]/verify after erase failed: %s\n", strerror(stat));
|
220 |
|
|
ok = false;
|
221 |
|
|
break;
|
222 |
|
|
}
|
223 |
|
|
flash_addr = flash_addr + l;
|
224 |
|
|
len -= l;
|
225 |
|
|
lp1 = (cyg_uint32 *)test_buf1;
|
226 |
|
|
for (i = 0; i < 0x200; i += sizeof(cyg_uint32)) {
|
227 |
|
|
if (*lp1++ != 0xFFFFFFFF) {
|
228 |
|
|
diag_printf("FLASH: non-erased data found at offset %p\n",
|
229 |
|
|
(cyg_uint8 *)flash_addr + (CYG_ADDRWORD)((lp1-1) - test_buf1));
|
230 |
|
|
diag_dump_buf((void *)(lp1-1), 32);
|
231 |
|
|
ok = false;
|
232 |
|
|
len = 0;
|
233 |
|
|
break;
|
234 |
|
|
}
|
235 |
|
|
}
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
if (!ok) {
|
239 |
|
|
CYG_TEST_INFO("FLASH erase failed");
|
240 |
|
|
passed = false;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
// Simple write/verify test
|
244 |
|
|
diag_printf("... Write/verify test\n");
|
245 |
|
|
lp1 = (cyg_uint32 *)test_buf1;
|
246 |
|
|
for (i = 0; i < FLASH_TEST_LENGTH; i += sizeof(cyg_uint32)) {
|
247 |
|
|
*lp1 = (cyg_uint32)lp1;
|
248 |
|
|
lp1++;
|
249 |
|
|
}
|
250 |
|
|
ok = true;
|
251 |
|
|
len = FLASH_TEST_LENGTH;
|
252 |
|
|
if (ok && (stat = cyg_io_bwrite(flash_handle, (void *)test_buf1,
|
253 |
|
|
&len, flash_test_start)) != 0) {
|
254 |
|
|
diag_printf("FLASH: write failed: %s\n", strerror(stat));
|
255 |
|
|
ok = false;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
len = FLASH_TEST_LENGTH;
|
260 |
|
|
if (ok && (stat = cyg_io_bread(flash_handle, (void *)test_buf2, &len, flash_test_start)) != CYG_FLASH_ERR_OK) {
|
261 |
|
|
diag_printf("FLASH: read/verify after write failed: %s\n", strerror(stat));
|
262 |
|
|
ok = false;
|
263 |
|
|
}
|
264 |
|
|
lp1 = (cyg_uint32 *)test_buf1;
|
265 |
|
|
lp2 = (cyg_uint32 *)test_buf2;
|
266 |
|
|
for (i = 0; i < FLASH_TEST_LENGTH; i += sizeof(cyg_uint32)) {
|
267 |
|
|
if (*lp2++ != *lp1++) {
|
268 |
|
|
diag_printf("FLASH: incorrect data found at offset %p\n", (void *)((CYG_ADDRWORD)(lp2-1) - test_buf2));
|
269 |
|
|
diag_dump_buf((void *)(lp2-1), 32);
|
270 |
|
|
ok = false;
|
271 |
|
|
break;
|
272 |
|
|
}
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
// Try reading in little pieces
|
276 |
|
|
len = FLASH_TEST_LENGTH;
|
277 |
|
|
flash_addr = flash_test_start;
|
278 |
|
|
lp1 = (cyg_uint32 *)test_buf1;
|
279 |
|
|
lp2 = (cyg_uint32 *)test_buf2;
|
280 |
|
|
while (len > 0) {
|
281 |
|
|
cyg_uint32 l = 0x200;
|
282 |
|
|
if ((stat = cyg_io_bread(flash_handle, (void *)lp2, &l, flash_addr)) != 0) {
|
283 |
|
|
diag_printf("FLASH: read[short]/verify after erase failed: %s\n", strerror(stat));
|
284 |
|
|
ok = false;
|
285 |
|
|
break;
|
286 |
|
|
}
|
287 |
|
|
flash_addr = flash_addr + l;
|
288 |
|
|
len -= l;
|
289 |
|
|
for (i = 0; i < l; i += sizeof(cyg_uint32)) {
|
290 |
|
|
if (*lp2++ != *lp1++) {
|
291 |
|
|
diag_printf("FLASH: incorrect data found at offset %p\n",
|
292 |
|
|
(cyg_uint8 *)flash_addr + (CYG_ADDRWORD)((lp2-1) - test_buf2));
|
293 |
|
|
diag_dump_buf((void *)(lp2-1), 32);
|
294 |
|
|
ok = false;
|
295 |
|
|
len = 0;
|
296 |
|
|
break;
|
297 |
|
|
}
|
298 |
|
|
}
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
|
302 |
|
|
// Lock test
|
303 |
|
|
diag_printf("... Lock test\n");
|
304 |
|
|
ok = true;
|
305 |
|
|
l.offset = flash_test_start;
|
306 |
|
|
l.len = FLASH_TEST_LENGTH;
|
307 |
|
|
len = sizeof(l);
|
308 |
|
|
if ((stat = cyg_io_get_config(flash_handle, CYG_IO_GET_CONFIG_FLASH_LOCK, &l, &len ) ) != 0 || l.flasherr != 0 )
|
309 |
|
|
{
|
310 |
|
|
diag_printf("FLASH: unlock failed: %s %s\n", strerror(stat), cyg_flash_errmsg(l.flasherr));
|
311 |
|
|
ok = false;
|
312 |
|
|
}
|
313 |
|
|
#endif
|
314 |
|
|
|
315 |
|
|
if (!ok) {
|
316 |
|
|
CYG_TEST_INFO("FLASH write/verify failed");
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
if (passed) {
|
320 |
|
|
CYG_TEST_PASS_FINISH("FLASH test1");
|
321 |
|
|
} else {
|
322 |
|
|
CYG_TEST_FAIL_FINISH("FLASH test1");
|
323 |
|
|
}
|
324 |
|
|
}
|
325 |
|
|
#endif
|
326 |
|
|
|
327 |
|
|
//=================================================================
|
328 |
|
|
// EOF flashdev.c
|