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#include <common.h>
26#include <nios.h>
27
28flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
29
30
31void flash_print_info (flash_info_t * info)
32{
33 int i, k;
34 unsigned long size;
35 int erased;
36 volatile unsigned char *flash;
37
38 printf (" Size: %ld KB in %d Sectors\n",
39 info->size >> 10, info->sector_count);
40 printf (" Sector Start Addresses:");
41 for (i = 0; i < info->sector_count; ++i) {
42
43
44 if (i != (info->sector_count - 1))
45 size = info->start[i + 1] - info->start[i];
46 else
47 size = info->start[0] + info->size - info->start[i];
48 erased = 1;
49 flash = (volatile unsigned char *) info->start[i];
50 for (k = 0; k < size; k++) {
51 if (*flash++ != 0xff) {
52 erased = 0;
53 break;
54 }
55 }
56
57
58 if ((i % 5) == 0)
59 printf ("\n ");
60 printf (" %08lX%s%s", info->start[i], erased ? " E" : " ",
61 info->protect[i] ? "RO " : " ");
62 }
63 printf ("\n");
64}
65
66
67
68
69int flash_erase (flash_info_t * info, int s_first, int s_last)
70{
71 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
72 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
73 int prot, sect;
74 unsigned oldpri;
75 ulong start;
76
77
78 if ((s_first < 0) || (s_first > s_last)) {
79 printf ("- no sectors to erase\n");
80 return 1;
81 }
82
83 prot = 0;
84 for (sect = s_first; sect <= s_last; ++sect) {
85 if (info->protect[sect]) {
86 prot++;
87 }
88 }
89 if (prot) {
90 printf ("- Warning: %d protected sectors will not be erased!\n",
91 prot);
92 } else {
93 printf ("\n");
94 }
95
96#ifdef DEBUG
97 for (sect = s_first; sect <= s_last; sect++) {
98 printf("- Erase: Sect: %i @ 0x%08x\n", sect, info->start[sect]);
99 }
100#endif
101
102
103
104
105
106 oldpri = ipri (3);
107
108
109
110
111
112
113 for (sect = s_first; sect <= s_last; sect++) {
114 if (info->protect[sect] == 0) {
115 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
116 *addr = 0xaa;
117 *addr = 0x55;
118 *addr = 0x80;
119 *addr = 0xaa;
120 *addr = 0x55;
121 *addr2 = 0x30;
122
123
124
125
126 ipri(oldpri);
127 start = get_timer (0);
128 while (*addr2 != 0xff) {
129 udelay (1000 * 1000);
130 putc ('.');
131 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
132 printf ("timeout\n");
133 return 1;
134 }
135 }
136 oldpri = ipri (3);
137 }
138 }
139
140 printf ("\n");
141
142
143 ipri (oldpri);
144
145 return 0;
146}
147
148
149
150
151
152
153
154
155int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
156{
157
158 vu_char *cmd = (vu_char *) info->start[0];
159 vu_char *dst = (vu_char *) addr;
160 unsigned char b;
161 unsigned oldpri;
162 ulong start;
163
164 while (cnt) {
165
166 b = *src;
167 if ((*dst & b) != b) {
168 printf ("%02x : %02x\n", *dst, b);
169 return (2);
170 }
171
172
173
174
175 oldpri = ipri (3);
176 *cmd = 0xaa;
177 *cmd = 0x55;
178 *cmd = 0xa0;
179 *dst = b;
180
181
182 start = get_timer (0);
183 while (*dst != b) {
184 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
185 ipri (oldpri);
186 return 1;
187 }
188 }
189 dst++;
190 src++;
191 cnt--;
192 ipri (oldpri);
193 }
194
195 return (0);
196}
197