summaryrefslogtreecommitdiff
path: root/target/u-boot/2009.01-rc1/u-boot-2009.01-rc1-016-cmd_mem.patch
blob: bac2f935f6d64f8e1135848067f4760214e6e954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
diff -urN u-boot-2009.01-0rig//common/cmd_mem.c u-boot-2009.01/common/cmd_mem.c
--- u-boot-2009.01-0rig//common/cmd_mem.c	2008-12-16 23:48:27.000000000 +0100
+++ u-boot-2009.01/common/cmd_mem.c	2009-01-04 13:27:03.000000000 +0100
@@ -37,11 +37,120 @@
 #endif
 #include <watchdog.h>
 
+#include <flash.h>
+#ifdef	CONFIG_SYS_NO_FLASH
+#define	in_flash(x)	0
+#else
+#define in_flash(x)	(addr2info(x)!=NULL)
+#endif
+
+#ifdef CONFIG_HAS_DATAFLASH
+#define in_dataflash(x)	addr_dataflash(x)
+#else
+#define in_dataflash(x)	0
+#endif
+
+#ifdef	CONFIG_CMD_MMC
+#define	in_SD_card(x)	(mmc2info(x) != 0)
+#else
+#define	in_SD_card(x)	0
+#endif
+
+
 #ifdef	CMD_MEM_DEBUG
 #define	PRINTF(fmt,args...)	printf (fmt ,##args)
 #else
 #define PRINTF(fmt,args...)
 #endif
+#if defined(CONFIG_HAS_DATAFLASH)
+#define DF_PAGE_VALID	0x01
+#define CONFIG_SYS_DATAFLASH_PAGE_SIZE 1056
+typedef	char dfpagebuf[CONFIG_SYS_DATAFLASH_PAGE_SIZE];
+dfpagebuf	dfcache[3];		// Cache buffers for dataflash
+unsigned char	dfvalid[3];			// if DF_PAGE_VALID is set, then the, cache buffer contents are valid 
+unsigned char	dflru[3];			// Least recently used algorithm to determine which buffer to reuse
+unsigned int		dfpage[3];		// TLB for cache, tells which page is loaded into dfcache
+
+
+void		flush_mem_cache(void)
+{
+	dfvalid[0] = dfvalid[1] = dfvalid[2] = 0;
+	dflru[0] = 0;
+	dflru[1] = 1;
+	dflru[2] = 2;
+}
+
+typedef	union {
+		uchar	byte[4];
+		ulong 	word;
+} reg;
+
+unsigned int replace(char *p, unsigned int data)
+{
+	ulong i;
+	reg	in;
+	reg	out;
+	
+	in.word = data;
+	for(i = 0; i <= 3; i++) {
+		out.byte[0] = *p;
+		*p++ = in.byte[0];
+	}
+	return out.word;
+}
+
+ulong	translate(unsigned int addr)
+{
+	unsigned int	page = (addr) / CONFIG_SYS_DATAFLASH_PAGE_SIZE;	
+	unsigned int	index = (addr) - (page * CONFIG_SYS_DATAFLASH_PAGE_SIZE);
+	unsigned int	i,rc;
+	for(i = 0; i < 2; i++) {
+		if((page == dfpage[i]) && (dfvalid[i] & DF_PAGE_VALID)) {
+			if(dflru[0] == i) {
+				// nothing
+			} else if(dflru[1]  == i) {
+				// swap with 0
+				dflru[1] = dflru[0];
+				dflru[0] = i;
+			} else {
+				// shift down
+				dflru[2] = dflru[1];
+				dflru[1] = dflru[0];
+				dflru[0] = i;
+			}
+			return (unsigned int) &dfcache[i][index];
+		}
+	}
+	// not found
+	// use buffer found in dflru[2]
+	i = dflru[2];
+	if ((rc = read_dataflash(addr, CONFIG_SYS_DATAFLASH_PAGE_SIZE, dfcache[i])) == DATAFLASH_OK){
+		dflru[2] = dflru[1];
+		dflru[1] = dflru[0];
+		dflru[0] = i;
+		dfvalid[i] |= DF_PAGE_VALID;
+		return  (unsigned int) &dfcache[i][index];
+	}
+	return 0;	
+}
+
+
+
+void		df_writed(unsigned char *ldest, ulong  laddr)
+{
+
+}
+void		df_writew(unsigned char *ldest,ushort laddr) {}
+void		df_writeb(unsigned char *ldest, u_char laddr) {}
+#endif
+
+#if defined(CONFIG_HAS_DATAFLASH)
+void	flush_mem_cache(void);
+ulong	translate(unsigned int addr);
+#else
+#define	flush_mem_cache(x)
+#define	translate(x) x
+#endif
 
 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
 
@@ -67,7 +176,8 @@
 	ulong	nbytes, linebytes;
 #endif
 	int	size;
-	int rc = 0;
+	int	rc = 0;
+	int	done = 0; 
 
 	/* We use the last specified parameters, unless new ones are
 	 * entered.
@@ -106,26 +216,29 @@
 	 * We buffer all read data, so we can make sure data is read only
 	 * once, and all accesses are with the specified bus width.
 	 */
-	nbytes = length * size;
-	do {
-		char	linebuf[DISP_LINE_LEN];
-		void* p;
-		linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
-
-		rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
-		p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
-		print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
-
-		nbytes -= linebytes;
-		addr += linebytes;
-		if (ctrlc()) {
-			rc = 1;
-			break;
-		}
-	} while (nbytes > 0);
-#else
+	if(in_dataflash(addr)) {
+		nbytes = length * size;
+		do {
+			char	linebuf[DISP_LINE_LEN];
+			void* p;
+			linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
+	
+			rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
+			p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
+			print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
+	
+			nbytes -= linebytes;
+			addr += linebytes;
+			if (ctrlc()) {
+				rc = 1;
+				break;
+			}
+		} while (nbytes > 0);
+		done = 1;
+	}
+#endif
 
-# if defined(CONFIG_BLACKFIN)
+#if defined(CONFIG_BLACKFIN)
 	/* See if we're trying to display L1 inst */
 	if (addr_bfin_on_chip_mem(addr)) {
 		char linebuf[DISP_LINE_LEN];
@@ -142,15 +255,15 @@
 				break;
 			}
 		} while (nbytes > 0);
-	} else
-# endif
+		done = 1;
+	}
+#endif
 
-	{
+	if (!done) {
 		/* Print the lines. */
 		print_buffer(addr, (void*)addr, size, length, DISP_LINE_LEN/size);
 		addr += size*length;
 	}
-#endif
 
 	dp_last_addr = addr;
 	dp_last_length = length;
@@ -273,6 +386,8 @@
 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	ulong	addr1, addr2, count, ngood;
+	ulong	laddr1, laddr2;
+
 	int	size;
 	int     rcode = 0;
 
@@ -294,12 +409,6 @@
 
 	count = simple_strtoul(argv[3], NULL, 16);
 
-#ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash(addr1) | addr_dataflash(addr2)){
-		puts ("Comparison with DataFlash space not supported.\n\r");
-		return 0;
-	}
-#endif
 
 #ifdef CONFIG_BLACKFIN
 	if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
@@ -308,9 +417,12 @@
 	}
 #endif
 
+	flush_mem_cache();
 	ngood = 0;
 
 	while (count-- > 0) {
+		laddr1	= translate((ulong) addr1);
+		laddr2	= translate((ulong) addr2);
 		if (size == 4) {
 			ulong word1 = *(ulong *)addr1;
 			ulong word2 = *(ulong *)addr2;
@@ -359,6 +471,14 @@
 {
 	ulong	addr, dest, count;
 	int	size;
+#ifdef	CONFIG_HAS_DATAFLASH
+	int rc;
+	unsigned int crc,oldcrc,use_crc;
+	unsigned int olddata;
+	char *p;
+	char *env_crc_check;
+	
+#endif
 
 	if (argc != 4) {
 		printf ("Usage:\n%s\n", cmdtp->usage);
@@ -385,13 +505,14 @@
 
 #ifndef CONFIG_SYS_NO_FLASH
 	/* check if we are copying to Flash */
-	if ( (addr2info(dest) != NULL)
-#ifdef CONFIG_HAS_DATAFLASH
-	   && (!addr_dataflash(dest))
-#endif
-	   ) {
+	if (in_flash(dest)) {
 		int rc;
-
+#if	defined(CONFIG_HAS_DATAFLASH) || defined(CONFIG_CMD_MMC)
+		if(in_dataflash(addr) || in_SD_card(addr)) {
+			puts ("Cannot copy to flash from storage...\n");
+			return 1;
+		}
+#endif
 		puts ("Copy to Flash... ");
 
 		rc = flash_write ((char *)addr, dest, count*size);
@@ -405,9 +526,15 @@
 #endif
 
 #if defined(CONFIG_CMD_MMC)
-	if (mmc2info(dest)) {
+	if (in_SD_card(dest)) {
 		int rc;
 
+#if	defined(CONFIG_HAS_DATAFLASH)
+		if(in_dataflash(addr) || in_SD_card(addr)) {
+			puts ("Cannot copy to flash-card from another storage location...\n");
+			return 1;
+		}
+#endif
 		puts ("Copy to MMC... ");
 		switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
 		case 0:
@@ -424,9 +551,12 @@
 		return 0;
 	}
 
-	if (mmc2info(addr)) {
+	if (in_SD_card(addr)) {
 		int rc;
-
+		if(in_flash(dest) || in_dataflash(dest) || in_SD_card(dest)) {
+			puts ("Cannot copy from flash-card from another storage location...\n");
+			return 1;
+		}
 		puts ("Copy from MMC... ");
 		switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
 		case 0:
@@ -445,39 +575,64 @@
 #endif
 
 #ifdef CONFIG_HAS_DATAFLASH
-	/* Check if we are copying from RAM or Flash to DataFlash */
-	if (addr_dataflash(dest) && !addr_dataflash(addr)){
-		int rc;
-
-		puts ("Copy to DataFlash... ");
-
-		rc = write_dataflash (dest, addr, count*size);
-
+	/* Check if we are copying from Dataflash to RAM */
+	/* Note: Cannot copy from Dataflash to Flash directly */
+	env_crc_check = getenv("crc-check");
+	use_crc = (strcmp(env_crc_check, "on") == 0);
+	if (in_dataflash(addr)) {
+		if(in_dataflash(dest) || in_flash(dest) || in_SD_card(dest)) {
+			puts ("Cannot copy to dataflash from another storage location...\n");
+			return 1;
+		}
+		puts ("Copy from DataFlash...");
+		rc = read_dataflash(addr, count * size, (char *) dest);
 		if (rc != 1) {
 			dataflash_perror (rc);
 			return (1);
 		}
-		puts ("done\n");
-		return 0;
-	}
 
-	/* Check if we are copying from DataFlash to RAM */
-	if (addr_dataflash(addr) && !addr_dataflash(dest)
-#ifndef CONFIG_SYS_NO_FLASH
-				 && (addr2info(dest) == NULL)
-#endif
-	   ){
-		int rc;
-		rc = read_dataflash(addr, count * size, (char *) dest);
+		if (use_crc) {
+			puts ("(Calculating CRC) ... ");
+			p = (char *) dest + (count * size) + 3;
+			olddata = replace(p,0);
+			rc = read_dataflash(addr, (count * size)+4, (char *) dest);
+			oldcrc = replace(p,olddata);
+			crc = crc32 (0, (const uchar *) dest, count*size);
+			if(crc != oldcrc) {
+				printf("\n\rBad CRC, %x expected, found %x... \n\r",oldcrc,crc);
+				return 1;
+			} else {
+				printf("[0x%x]",crc);
+			}
+		}
+		puts ("done\n\r");
+		return 0;
+	} else 	if (!in_dataflash(addr) && in_dataflash(dest)) {
+		if(in_dataflash(addr) || in_SD_card(addr)) {
+			puts ("Cannot copy to dataflash from another storage location...\n");
+			return 1;
+		}
+		puts ("Copy to DataFlash... ");
+		if (use_crc) {
+			puts ("(Calulating CRC) ... ");
+			crc = crc32 (0, (const uchar *) addr, count*size);
+			// Save CRC in small endian format
+			printf("= 0x%08x written to [%08x] ... ",crc,(unsigned int) (dest + count * size));
+			p = (char *) addr+(count*size);
+			olddata = replace(p,crc);
+			rc = write_dataflash (dest, addr, count*size+4);
+			crc = replace((char *) addr+(count*size),olddata);
+		} else {
+			rc = write_dataflash (dest, addr, count*size);
+		}
 		if (rc != 1) {
 			dataflash_perror (rc);
 			return (1);
 		}
+		puts ("done\n");
 		return 0;
-	}
-
-	if (addr_dataflash(addr) && addr_dataflash(dest)){
-		puts ("Unsupported combination of source/destination.\n\r");
+	} else 	if (in_dataflash(addr) && in_dataflash(dest)){
+		puts ("Cannot copy between two dataflash areas\n\r");
 		return 1;
 	}
 #endif
@@ -490,6 +645,7 @@
 	}
 #endif
 
+	/* Copy between RAM locations, or parallell flash to RAM */
 	while (count-- > 0) {
 		if (size == 4)
 			*((ulong  *)dest) = *((ulong  *)addr);
@@ -1011,7 +1167,7 @@
 	}
 
 #ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash(addr)){
+	if (in_dataflash(addr)){
 		puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
 		return 0;
 	}