altos: Remove 8051 address space specifiers
[fw/altos] / src / kernel / ao_storage.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_storage.h>
21
22 uint8_t
23 ao_storage_read(ao_pos_t pos, void *buf, uint16_t len) 
24 {
25 #ifdef CC1111
26         return ao_storage_device_read(pos, buf, len);
27 #else
28         uint16_t this_len;
29         uint16_t this_off;
30
31         ao_storage_setup();
32         if (pos >= ao_storage_total || pos + len > ao_storage_total)
33                 return 0;
34         while (len) {
35
36                 /* Compute portion of transfer within
37                  * a single block
38                  */
39                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
40                 this_len = ao_storage_unit - this_off;
41                 if (this_len > len)
42                         this_len = len;
43
44                 if (!ao_storage_device_read(pos, buf, this_len))
45                         return 0;
46
47                 /* See how much is left */
48                 buf += this_len;
49                 len -= this_len;
50                 pos += this_len;
51         }
52         return 1;
53 #endif
54 }
55
56 uint8_t
57 ao_storage_write(ao_pos_t pos, void *buf, uint16_t len) 
58 {
59 #ifdef CC1111
60         return ao_storage_device_write(pos, buf, len);
61 #else
62         uint16_t this_len;
63         uint16_t this_off;
64
65         ao_storage_setup();
66         if (pos >= ao_storage_total || pos + len > ao_storage_total)
67                 return 0;
68         while (len) {
69
70                 /* Compute portion of transfer within
71                  * a single block
72                  */
73                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
74                 this_len = ao_storage_unit - this_off;
75                 if (this_len > len)
76                         this_len = len;
77
78                 if (!ao_storage_device_write(pos, buf, this_len))
79                         return 0;
80
81                 /* See how much is left */
82                 buf += this_len;
83                 len -= this_len;
84                 pos += this_len;
85         }
86         return 1;
87 #endif
88 }
89
90 static uint8_t storage_data[128];
91
92 static void
93 ao_storage_dump(void) 
94 {
95         uint8_t i, j;
96
97         ao_cmd_hex();
98         if (ao_cmd_status != ao_cmd_success)
99                 return;
100         for (i = 0; ; i += 8) {
101                 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
102                                   storage_data,
103                                   8)) {
104                         ao_cmd_put16((uint16_t) i);
105                         for (j = 0; j < 8; j++) {
106                                 putchar(' ');
107                                 ao_cmd_put8(storage_data[j]);
108                         }
109                         putchar ('\n');
110                 }
111                 if (i == 248)
112                         break;
113         }
114 }
115
116 #if HAS_STORAGE_DEBUG
117
118 /* not enough space for this today
119  */
120 static void
121 ao_storage_store(void) 
122 {
123         uint16_t block;
124         uint8_t i;
125         uint16_t len;
126         static uint8_t b;
127         uint32_t addr;
128
129         ao_cmd_hex();
130         block = ao_cmd_lex_i;
131         ao_cmd_hex();
132         i = ao_cmd_lex_i;
133         addr = ((uint32_t) block << 8) | i;
134         ao_cmd_hex();
135         len = ao_cmd_lex_i;
136         if (ao_cmd_status != ao_cmd_success)
137                 return;
138         while (len--) {
139                 ao_cmd_hex();
140                 if (ao_cmd_status != ao_cmd_success)
141                         return;
142                 b = ao_cmd_lex_i;
143                 ao_storage_write(addr, &b, 1);
144                 addr++;
145         }
146 }
147 #endif
148
149 void
150 ao_storage_zap(void) 
151 {
152         ao_cmd_hex();
153         if (ao_cmd_status != ao_cmd_success)
154                 return;
155         ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
156 }
157
158 void
159 ao_storage_zapall(void) 
160 {
161         uint32_t        pos;
162
163         ao_cmd_white();
164         if (!ao_match_word("DoIt"))
165                 return;
166         for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
167                 ao_storage_erase(pos);
168 }
169
170 #if AO_STORAGE_TEST
171
172 static void
173 ao_storage_failure(uint32_t pos, char *format, ...)
174 {
175         va_list a;
176         printf("TEST FAILURE AT %08x: ", pos);
177         va_start(a, format);
178         vprintf(format, a);
179         va_end(a);
180 }
181
182 static uint8_t
183 ao_storage_check_block(uint32_t pos, uint8_t value)
184 {
185         uint32_t        offset;
186         uint32_t        byte;
187
188         for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
189                 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
190                         ao_storage_failure(pos + offset, "read failed\n");
191                         return 0;
192                 }
193                 for (byte = 0; byte < sizeof (storage_data); byte++)
194                         if (storage_data[byte] != value) {
195                                 ao_storage_failure(pos + offset + byte,
196                                                    "want %02x got %02x\n",
197                                                    value, storage_data[byte]);
198                                 return 0;
199                         }
200         }
201         return 1;
202 }
203
204 static uint8_t
205 ao_storage_fill_block(uint32_t pos, uint8_t value)
206 {
207         uint32_t        offset;
208         uint32_t        byte;
209
210         for (byte = 0; byte < sizeof (storage_data); byte++)
211                 storage_data[byte] = value;
212         for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
213                 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
214                         ao_storage_failure(pos + offset, "write failed\n");
215                         return 0;
216                 }
217         }
218         return 1;
219 }
220
221 static uint8_t
222 ao_storage_check_incr_block(uint32_t pos)
223 {
224         uint32_t        offset;
225         uint32_t        byte;
226
227         for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
228                 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
229                         ao_storage_failure(pos + offset, "read failed\n");
230                         return 0;
231                 }
232                 for (byte = 0; byte < sizeof (storage_data); byte++) {
233                         uint8_t value = offset + byte;
234                         if (storage_data[byte] != value) {
235                                 ao_storage_failure(pos + offset + byte,
236                                                    "want %02x got %02x\n",
237                                                    value, storage_data[byte]);
238                                 return 0;
239                         }
240                 }
241         }
242         return 1;
243 }
244
245 static uint8_t
246 ao_storage_fill_incr_block(uint32_t pos)
247 {
248         uint32_t        offset;
249         uint32_t        byte;
250
251         for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
252                 for (byte = 0; byte < sizeof (storage_data); byte++)
253                         storage_data[byte] = offset + byte;
254                 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
255                         ao_storage_failure(pos + offset, "write failed\n");
256                         return 0;
257                 }
258         }
259         return 1;
260 }
261
262 static uint8_t
263 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
264 {
265         return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
266 }
267
268 static uint8_t
269 ao_storage_incr_check_block(uint32_t pos)
270 {
271         return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
272 }
273
274 static uint8_t
275 ao_storage_test_block(uint32_t pos) 
276 {
277         ao_storage_erase(pos);
278         printf(" erase"); flush();
279         if (!ao_storage_check_block(pos, 0xff))
280                 return 0;
281         printf(" zero"); flush();
282         if (!ao_storage_fill_check_block(pos, 0x00))
283                 return 0;
284         ao_storage_erase(pos);
285         printf(" 0xaa"); flush();
286         if (!ao_storage_fill_check_block(pos, 0xaa))
287                 return 0;
288         ao_storage_erase(pos);
289         printf(" 0x55"); flush();
290         if (!ao_storage_fill_check_block(pos, 0x55))
291                 return 0;
292         ao_storage_erase(pos);
293         printf(" increment"); flush();
294         if (!ao_storage_incr_check_block(pos))
295                 return 0;
296         ao_storage_erase(pos);
297         printf(" pass\n"); flush();
298         return 1;
299 }
300
301 static void
302 ao_storage_test(void) 
303 {
304         uint32_t        pos;
305
306         ao_cmd_white();
307         if (!ao_match_word("DoIt"))
308                 return;
309         for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
310                 printf("Testing block 0x%08x:", pos); flush();
311                 if (!ao_storage_test_block(pos))
312                         break;
313         }
314         printf("Test complete\n");
315 }
316 #endif /* AO_STORAGE_TEST */
317
318 void
319 ao_storage_info(void) 
320 {
321         ao_storage_setup();
322         printf("Storage size: %ld\n", (long) ao_storage_total);
323         printf("Storage erase unit: %ld\n", (long) ao_storage_block);
324         ao_storage_device_info();
325 }
326
327 const struct ao_cmds ao_storage_cmds[] = {
328         { ao_storage_info, "f\0Show storage" },
329         { ao_storage_dump, "e <block>\0Dump flash" },
330 #if HAS_STORAGE_DEBUG
331         { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
332 #endif
333         { ao_storage_zap, "z <block>\0Erase <block>" },
334         { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
335 #if AO_STORAGE_TEST
336         { ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
337 #endif
338         { 0, NULL },
339 };
340
341 void
342 ao_storage_init(void)
343 {
344         ao_storage_device_init();
345         ao_cmd_register(&ao_storage_cmds[0]);
346 }