FreeCalypso > hg > freecalypso-sw
annotate loadtools/flutil.c @ 61:a10491da8c3a
fc-loadtool flash support: sector table generation implemented
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Mon, 24 Jun 2013 17:46:15 +0000 |
parents | d98137625c0d |
children | 6fb41cfa773d |
rev | line source |
---|---|
56
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * Miscellaneous utility functions for flash support |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 */ |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 #include <sys/types.h> |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <stdio.h> |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <stdint.h> |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <stdlib.h> |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include "flash.h" |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 extern struct flash_bank_info flash_bank_info[2]; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 compute_flash_totsize_nsecs(bank) |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 { |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 struct flash_bank_info *bi; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 struct flash_region_desc *reg; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 bi = flash_bank_info + bank; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 for (reg = bi->bank_desc->regions; reg->nsectors; reg++) { |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 bi->nsectors += reg->nsectors; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 bi->total_size += reg->sector_size * reg->nsectors; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 } |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 } |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 /* the following function is used to verify that total_size is a power of 2 */ |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 count_ones(word) |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 uint32_t word; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 { |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 int count; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 for (count = 0; word; word >>= 1) |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 count += word & 1; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 return count; |
d98137625c0d
fc-loadtool flash: total_size logic implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 } |
61
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
35 |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
36 get_flash_sector_table(bank) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
37 { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
38 struct flash_bank_info *bi; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
39 struct flash_region_desc *reg; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
40 struct sector_info *sp; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
41 uint32_t offset; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
42 int i; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
43 |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
44 bi = flash_bank_info + bank; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
45 if (bi->sectors) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
46 return(0); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
47 sp = (struct sector_info *) malloc(sizeof(struct sector_info) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
48 * (bi->nsectors + 1)); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
49 if (!sp) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
50 fprintf(stderr, |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
51 "unable to malloc buffer for flash bank %d sector table\n", |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
52 bank); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
53 return(-1); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
54 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
55 bi->sectors = sp; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
56 /* now fill it */ |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
57 offset = 0; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
58 for (reg = bi->bank_desc->regions; reg->nsectors; reg++) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
59 for (i = 0; i < reg->nsectors; i++) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
60 sp->start = offset; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
61 sp->size = reg->sector_size; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
62 sp++; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
63 offset += reg->sector_size; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
64 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
65 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
66 /* sanity checks */ |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
67 if (sp - bi->sectors != bi->nsectors) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
68 fprintf(stderr, |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
69 "BUG in get_flash_sector_table(): wrong # of sectors at the end\n"); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
70 abort(); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
71 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
72 if (offset != bi->total_size) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
73 fprintf(stderr, |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
74 "BUG in get_flash_sector_table(): wrong offset at the end\n"); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
75 abort(); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
76 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
77 /* finish */ |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
78 sp->start = 0; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
79 sp->size = 0; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
80 return(0); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
81 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
82 |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
83 flashcmd_sectors(argc, argv, bank) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
84 char **argv; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
85 { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
86 struct flash_bank_info *bi; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
87 struct sector_info *sp; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
88 |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
89 if (argc > 2) { |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
90 fprintf(stderr, "error: too many arguments\n"); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
91 return(-1); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
92 } |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
93 if (get_flash_sector_table(bank) < 0) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
94 return(-1); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
95 bi = flash_bank_info + bank; |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
96 printf("%u sectors in flash bank %d:\n", bi->nsectors, bank); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
97 printf("Offset Size\n"); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
98 for (sp = bi->sectors; sp->size; sp++) |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
99 printf("%08lX %lx\n", (u_long) sp->start, (u_long) sp->size); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
100 return(0); |
a10491da8c3a
fc-loadtool flash support: sector table generation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
56
diff
changeset
|
101 } |