2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
24 #define YNORSIM_FNAME "emfile-nor"
26 /* Set YNORSIM_BIT_CHANGES to a a value from 1..30 to
27 *simulate bit flipping as the programming happens.
28 * A low value results in faster simulation with less chance of encountering a partially programmed
32 //#define YNORSIM_BIT_CHANGES 15
33 #define YNORSIM_BIT_CHANGES 2
36 /* Simulate 32MB of flash in 256k byte blocks.
40 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
41 #define YNORSIM_DEV_SIZE_U32 (32*1024 * 1024/4)
43 /* Simulate 8MB of flash in 256k byte blocks.
47 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
48 #define YNORSIM_DEV_SIZE_U32 (8*1024 * 1024/4)
51 static u32 word[YNORSIM_DEV_SIZE_U32];
53 extern int random_seed;
54 extern int simulate_power_failure;
56 static void NorError(void)
58 printf("Nor error\n");
62 static void ynorsim_save_image(void)
64 int h = open(YNORSIM_FNAME, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
65 write(h,word,sizeof(word));
69 static void ynorsim_restore_image(void)
71 int h = open(YNORSIM_FNAME, O_RDONLY, S_IREAD | S_IWRITE);
72 memset(word,0xFF,sizeof(word));
73 read(h,word,sizeof(word));
78 static void ynorsim_power_fail(void)
84 static int initialised = 0;
85 static int remaining_ops;
86 static int nops_so_far;
88 int ops_multiplier = 500;
90 static void ynorsim_maybe_power_fail(void)
97 if(simulate_power_failure &&
99 printf("Simulated power failure after %d operations\n",nops_so_far);
100 ynorsim_power_fail();
104 static void ynorsim_ready(void)
109 remaining_ops = 1000000000;
110 remaining_ops = (rand() % 10000) * ops_multiplier * YNORSIM_BIT_CHANGES;
111 ynorsim_restore_image();
114 void ynorsim_rd32(u32 *addr,u32 *buf, int nwords)
124 void ynorsim_wr_one_word32(u32 *addr,u32 val)
132 // Fail due to trying to change a zero into a 1
133 printf("attempt to set a zero to one (%x)->(%x)\n",tmp,val);
137 for(i = 0; i < YNORSIM_BIT_CHANGES; i++){
138 m = 1 << (rand() & 31);
142 ynorsim_maybe_power_fail();
148 ynorsim_maybe_power_fail();
151 void ynorsim_wr32(u32 *addr, u32 *buf, int nwords)
154 ynorsim_wr_one_word32(addr,*buf);
161 void ynorsim_erase(u32 *addr)
163 /* Todo... bit flipping */
164 memset(addr,0xFF,YNORSIM_BLOCK_SIZE_U32 * 4);
167 void ynorsim_initialise(void)
172 void ynorsim_shutdown(void)
174 ynorsim_save_image();
178 u32 *ynorsim_get_base(void)