FreeCalypso > hg > freecalypso-sw
comparison loadtools/flerase.c @ 62:6fb41cfa773d
fc-loadtool: flash erase implemented, compiles
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Thu, 27 Jun 2013 04:56:17 +0000 |
parents | |
children | cd12d1049f91 |
comparison
equal
deleted
inserted
replaced
61:a10491da8c3a | 62:6fb41cfa773d |
---|---|
1 /* | |
2 * This module implements the flash erase operation | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <stdio.h> | |
7 #include <stdint.h> | |
8 #include <stdlib.h> | |
9 #include <time.h> | |
10 #include "flash.h" | |
11 | |
12 extern struct flash_bank_info flash_bank_info[2]; | |
13 | |
14 do_sector_erase(bi, sp) | |
15 struct flash_bank_info *bi; | |
16 struct sector_info *sp; | |
17 { | |
18 int stat; | |
19 uint16_t flstat; | |
20 time_t start_time, curtime; | |
21 | |
22 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA); | |
23 if (stat) { | |
24 bad_w16: fprintf(stderr, | |
25 "unexpected response to w16 in erase cmd sequence - aborting\n"); | |
26 return(-1); | |
27 } | |
28 stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55); | |
29 if (stat) | |
30 goto bad_w16; | |
31 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x80); | |
32 if (stat) | |
33 goto bad_w16; | |
34 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA); | |
35 if (stat) | |
36 goto bad_w16; | |
37 stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55); | |
38 if (stat) | |
39 goto bad_w16; | |
40 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x30); | |
41 if (stat) | |
42 goto bad_w16; | |
43 start_time = time(0); | |
44 for (;;) { | |
45 stat = do_r16(bi->base_addr + sp->start, &flstat); | |
46 if (stat) | |
47 return(stat); /* error msg already printed */ | |
48 if (flstat == 0xFFFF) | |
49 return(0); | |
50 curtime = time(0); | |
51 if (curtime >= start_time + 20) { | |
52 fprintf(stderr, "erase timeout, aborting\n"); | |
53 return(-1); | |
54 } | |
55 } | |
56 } | |
57 | |
58 flashcmd_erase(argc, argv, bank) | |
59 char **argv; | |
60 { | |
61 struct flash_bank_info *bi; | |
62 u_long offset, len; | |
63 char *strtoul_endp; | |
64 struct sector_info *startsec, *endsec, *sp; | |
65 int stat; | |
66 | |
67 if (argc != 4) { | |
68 inv: fprintf(stderr, "usage: %s %s hex-start-offset hex-length\n", | |
69 argv[0], argv[1]); | |
70 return(-1); | |
71 } | |
72 offset = strtoul(argv[2], &strtoul_endp, 16); | |
73 if (*strtoul_endp) | |
74 goto inv; | |
75 bi = flash_bank_info + bank; | |
76 if (offset >= bi->total_size) { | |
77 fprintf(stderr, | |
78 "error: specified offset exceeds flash bank size (0x%lx)\n", | |
79 (u_long) bi->total_size); | |
80 return(-1); | |
81 } | |
82 len = strtoul(argv[3], &strtoul_endp, 16); | |
83 if (*strtoul_endp) | |
84 goto inv; | |
85 if (len > bi->total_size - offset) { | |
86 fprintf(stderr, | |
87 "error: specified offset+length exceed flash bank size (0x%lx)\n", | |
88 (u_long) bi->total_size); | |
89 return(-1); | |
90 } | |
91 if (!len) { | |
92 printf("Zero length specified - nothing to do!\n"); | |
93 return(0); | |
94 } | |
95 /* now enforce sector alignment for both offset and length */ | |
96 if (get_flash_sector_table(bank) < 0) | |
97 return(-1); | |
98 if (get_flash_sector_range(bi, offset, len, &startsec, &endsec) < 0) | |
99 return(-1); | |
100 printf("Erasing %d sector(s)\n", endsec - startsec); | |
101 for (sp = startsec; sp < endsec; sp++) { | |
102 stat = do_sector_erase(bi, sp); | |
103 if (stat) | |
104 return(stat); | |
105 putchar('.'); | |
106 fflush(stdout); | |
107 } | |
108 putchar('\n'); | |
109 return(0); | |
110 } |