]>
Commit | Line | Data |
---|---|---|
ea8015b8 | 1 | /* |
dc7c9a1a WD |
2 | * (C) Copyright 2001 |
3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net | |
4 | * | |
5 | * (C) Copyright 2001 | |
6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
ea8015b8 WD |
7 | * |
8 | * See file CREDITS for list of people who contributed to this | |
9 | * project. | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 of | |
14 | * the License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
24 | * MA 02111-1307 USA | |
25 | */ | |
26 | ||
27 | #include <common.h> | |
dc7c9a1a | 28 | #include <linux/byteorder/swab.h> |
ea8015b8 WD |
29 | |
30 | ||
dc7c9a1a | 31 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
ea8015b8 | 32 | |
dc7c9a1a WD |
33 | /* Board support for 1 or 2 flash devices */ |
34 | #undef FLASH_PORT_WIDTH32 | |
35 | #define FLASH_PORT_WIDTH16 | |
ea8015b8 | 36 | |
dc7c9a1a WD |
37 | #ifdef FLASH_PORT_WIDTH16 |
38 | #define FLASH_PORT_WIDTH ushort | |
39 | #define FLASH_PORT_WIDTHV vu_short | |
40 | #define SWAP(x) __swab16(x) | |
41 | #else | |
42 | #define FLASH_PORT_WIDTH ulong | |
43 | #define FLASH_PORT_WIDTHV vu_long | |
44 | #define SWAP(x) __swab32(x) | |
45 | #endif | |
ea8015b8 | 46 | |
dc7c9a1a WD |
47 | #define FPW FLASH_PORT_WIDTH |
48 | #define FPWV FLASH_PORT_WIDTHV | |
ea8015b8 | 49 | |
dc7c9a1a | 50 | #define mb() __asm__ __volatile__ ("" : : : "memory") |
ea8015b8 | 51 | |
dc7c9a1a WD |
52 | /*----------------------------------------------------------------------- |
53 | * Functions | |
54 | */ | |
55 | static ulong flash_get_size (FPW *addr, flash_info_t *info); | |
56 | static int write_data (flash_info_t *info, ulong dest, FPW data); | |
57 | static void flash_get_offsets (ulong base, flash_info_t *info); | |
58 | void inline spin_wheel(void); | |
ea8015b8 WD |
59 | |
60 | /*----------------------------------------------------------------------- | |
61 | */ | |
62 | ||
dc7c9a1a | 63 | unsigned long flash_init (void) |
ea8015b8 | 64 | { |
dc7c9a1a | 65 | int i; |
ea8015b8 WD |
66 | ulong size = 0; |
67 | ||
68 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) | |
69 | { | |
8bde7f77 WD |
70 | switch (i) |
71 | { | |
72 | case 0: | |
73 | flash_get_size((FPW *)PHYS_FLASH_1, &flash_info[i]); | |
74 | flash_get_offsets(PHYS_FLASH_1, &flash_info[i]); | |
75 | break; | |
76 | default: | |
77 | panic("configured to many flash banks!\n"); | |
78 | break; | |
79 | } | |
ea8015b8 WD |
80 | size += flash_info[i].size; |
81 | } | |
82 | ||
83 | /* Protect monitor and environment sectors | |
84 | */ | |
85 | flash_protect(FLAG_PROTECT_SET, | |
86 | CFG_FLASH_BASE, | |
3b57fe0a | 87 | CFG_FLASH_BASE + monitor_flash_len - 1, |
ea8015b8 WD |
88 | &flash_info[0]); |
89 | ||
90 | flash_protect(FLAG_PROTECT_SET, | |
91 | CFG_ENV_ADDR, | |
92 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, | |
93 | &flash_info[0]); | |
94 | ||
95 | return size; | |
96 | } | |
97 | ||
98 | /*----------------------------------------------------------------------- | |
99 | */ | |
dc7c9a1a | 100 | static void flash_get_offsets (ulong base, flash_info_t *info) |
ea8015b8 | 101 | { |
dc7c9a1a | 102 | int i; |
ea8015b8 | 103 | |
dc7c9a1a WD |
104 | if (info->flash_id == FLASH_UNKNOWN) { |
105 | return; | |
ea8015b8 | 106 | } |
ea8015b8 | 107 | |
dc7c9a1a WD |
108 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
109 | for (i = 0; i < info->sector_count; i++) { | |
110 | info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE); | |
111 | info->protect[i] = 0; | |
112 | } | |
113 | } | |
ea8015b8 WD |
114 | } |
115 | ||
116 | /*----------------------------------------------------------------------- | |
117 | */ | |
dc7c9a1a | 118 | void flash_print_info (flash_info_t *info) |
ea8015b8 | 119 | { |
dc7c9a1a | 120 | int i; |
ea8015b8 | 121 | |
dc7c9a1a WD |
122 | if (info->flash_id == FLASH_UNKNOWN) { |
123 | printf ("missing or unknown FLASH type\n"); | |
124 | return; | |
8bde7f77 | 125 | } |
ea8015b8 | 126 | |
dc7c9a1a WD |
127 | switch (info->flash_id & FLASH_VENDMASK) { |
128 | case FLASH_MAN_INTEL: printf ("INTEL "); break; | |
129 | default: printf ("Unknown Vendor "); break; | |
ea8015b8 WD |
130 | } |
131 | ||
dc7c9a1a WD |
132 | switch (info->flash_id & FLASH_TYPEMASK) { |
133 | case FLASH_28F128J3A: | |
134 | printf ("28F128J3A\n"); break; | |
135 | default: printf ("Unknown Chip Type\n"); break; | |
8bde7f77 | 136 | } |
dc7c9a1a WD |
137 | |
138 | printf (" Size: %ld MB in %d Sectors\n", | |
8bde7f77 | 139 | info->size >> 20, info->sector_count); |
dc7c9a1a WD |
140 | |
141 | printf (" Sector Start Addresses:"); | |
142 | for (i=0; i<info->sector_count; ++i) { | |
8bde7f77 WD |
143 | if ((i % 5) == 0) |
144 | printf ("\n "); | |
dc7c9a1a WD |
145 | printf (" %08lX%s", |
146 | info->start[i], | |
147 | info->protect[i] ? " (RO)" : " " | |
148 | ); | |
8bde7f77 WD |
149 | } |
150 | printf ("\n"); | |
dc7c9a1a WD |
151 | return; |
152 | } | |
ea8015b8 | 153 | |
dc7c9a1a WD |
154 | /* |
155 | * The following code cannot be run from FLASH! | |
156 | */ | |
157 | static ulong flash_get_size (FPW *addr, flash_info_t *info) | |
158 | { | |
159 | volatile FPW value; | |
160 | /* Write auto select command: read Manufacturer ID */ | |
161 | addr[0x5555] = (FPW)0x00AA00AA; | |
162 | addr[0x2AAA] = (FPW)0x00550055; | |
163 | addr[0x5555] = (FPW)0x00900090; | |
164 | ||
165 | mb(); | |
166 | value = addr[0]; | |
8bde7f77 | 167 | |
dc7c9a1a WD |
168 | switch (value) { |
169 | ||
170 | case (FPW)INTEL_MANUFACT: | |
171 | info->flash_id = FLASH_MAN_INTEL; | |
172 | break; | |
173 | ||
174 | default: | |
175 | info->flash_id = FLASH_UNKNOWN; | |
176 | info->sector_count = 0; | |
177 | info->size = 0; | |
178 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ | |
179 | return (0); /* no or unknown flash */ | |
ea8015b8 WD |
180 | } |
181 | ||
dc7c9a1a WD |
182 | mb(); |
183 | value = addr[1]; /* device ID */ | |
184 | switch (value) { | |
ea8015b8 | 185 | |
dc7c9a1a WD |
186 | case (FPW)INTEL_ID_28F128J3A: |
187 | info->flash_id += FLASH_28F128J3A; | |
188 | info->sector_count = 128; | |
189 | info->size = 0x02000000; | |
190 | break; /* => 16 MB */ | |
ea8015b8 | 191 | |
dc7c9a1a WD |
192 | default: |
193 | info->flash_id = FLASH_UNKNOWN; | |
194 | break; | |
ea8015b8 WD |
195 | } |
196 | ||
dc7c9a1a WD |
197 | if (info->sector_count > CFG_MAX_FLASH_SECT) { |
198 | printf ("** ERROR: sector count %d > max (%d) **\n", | |
199 | info->sector_count, CFG_MAX_FLASH_SECT); | |
200 | info->sector_count = CFG_MAX_FLASH_SECT; | |
201 | } | |
202 | ||
203 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ | |
ea8015b8 | 204 | |
dc7c9a1a | 205 | return (info->size); |
ea8015b8 WD |
206 | } |
207 | ||
dc7c9a1a | 208 | |
ea8015b8 WD |
209 | /*----------------------------------------------------------------------- |
210 | */ | |
211 | ||
212 | int flash_erase (flash_info_t *info, int s_first, int s_last) | |
213 | { | |
dc7c9a1a WD |
214 | int flag, prot, sect; |
215 | ulong type, start, last; | |
216 | int rcode = 0; | |
ea8015b8 WD |
217 | |
218 | if ((s_first < 0) || (s_first > s_last)) { | |
dc7c9a1a WD |
219 | if (info->flash_id == FLASH_UNKNOWN) { |
220 | printf ("- missing\n"); | |
221 | } else { | |
222 | printf ("- no sectors to erase\n"); | |
223 | } | |
224 | return 1; | |
ea8015b8 WD |
225 | } |
226 | ||
dc7c9a1a WD |
227 | type = (info->flash_id & FLASH_VENDMASK); |
228 | if ((type != FLASH_MAN_INTEL)) { | |
229 | printf ("Can't erase unknown flash type %08lx - aborted\n", | |
230 | info->flash_id); | |
231 | return 1; | |
ea8015b8 WD |
232 | } |
233 | ||
234 | prot = 0; | |
235 | for (sect=s_first; sect<=s_last; ++sect) { | |
236 | if (info->protect[sect]) { | |
237 | prot++; | |
238 | } | |
239 | } | |
ea8015b8 | 240 | |
dc7c9a1a WD |
241 | if (prot) { |
242 | printf ("- Warning: %d protected sectors will not be erased!\n", | |
243 | prot); | |
244 | } else { | |
245 | printf ("\n"); | |
246 | } | |
247 | ||
248 | start = get_timer (0); | |
249 | last = start; | |
250 | ||
251 | /* Disable interrupts which might cause a timeout here */ | |
252 | flag = disable_interrupts(); | |
ea8015b8 WD |
253 | |
254 | /* Start erase on unprotected sectors */ | |
dc7c9a1a WD |
255 | for (sect = s_first; sect<=s_last; sect++) { |
256 | if (info->protect[sect] == 0) { /* not protected */ | |
257 | FPWV *addr = (FPWV *)(info->start[sect]); | |
258 | FPW status; | |
259 | ||
ea8015b8 WD |
260 | printf("Erasing sector %2d ... ", sect); |
261 | ||
262 | /* arm simple, non interrupt dependent timer */ | |
263 | reset_timer_masked(); | |
264 | ||
dc7c9a1a WD |
265 | *addr = (FPW)0x00500050; /* clear status register */ |
266 | *addr = (FPW)0x00200020; /* erase setup */ | |
267 | *addr = (FPW)0x00D000D0; /* erase confirm */ | |
268 | ||
269 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { | |
270 | if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) { | |
271 | printf ("Timeout\n"); | |
272 | *addr = (FPW)0x00B000B0; /* suspend erase */ | |
273 | *addr = (FPW)0x00FF00FF; /* reset to read mode */ | |
274 | rcode = 1; | |
275 | break; | |
ea8015b8 WD |
276 | } |
277 | } | |
278 | ||
dc7c9a1a WD |
279 | *addr = (FPW)0x00500050; /* clear status register cmd. */ |
280 | *addr = (FPW)0x00FF00FF; /* resest to read mode */ | |
ea8015b8 | 281 | |
dc7c9a1a | 282 | printf (" done\n"); |
8bde7f77 WD |
283 | } |
284 | } | |
dc7c9a1a | 285 | return rcode; |
ea8015b8 WD |
286 | } |
287 | ||
288 | /*----------------------------------------------------------------------- | |
dc7c9a1a WD |
289 | * Copy memory to flash, returns: |
290 | * 0 - OK | |
291 | * 1 - write timeout | |
292 | * 2 - Flash not erased | |
293 | * 4 - Flash not identified | |
ea8015b8 WD |
294 | */ |
295 | ||
296 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) | |
297 | { | |
dc7c9a1a WD |
298 | ulong cp, wp; |
299 | FPW data; | |
300 | int count, i, l, rc, port_width; | |
ea8015b8 | 301 | |
dc7c9a1a WD |
302 | if (info->flash_id == FLASH_UNKNOWN) { |
303 | return 4; | |
304 | } | |
305 | /* get lower word aligned address */ | |
306 | #ifdef FLASH_PORT_WIDTH16 | |
307 | wp = (addr & ~1); | |
308 | port_width = 2; | |
309 | #else | |
310 | wp = (addr & ~3); | |
311 | port_width = 4; | |
312 | #endif | |
ea8015b8 WD |
313 | |
314 | /* | |
315 | * handle unaligned start bytes | |
316 | */ | |
317 | if ((l = addr - wp) != 0) { | |
318 | data = 0; | |
319 | for (i=0, cp=wp; i<l; ++i, ++cp) { | |
dc7c9a1a | 320 | data = (data << 8) | (*(uchar *)cp); |
ea8015b8 | 321 | } |
dc7c9a1a WD |
322 | for (; i<port_width && cnt>0; ++i) { |
323 | data = (data << 8) | *src++; | |
ea8015b8 WD |
324 | --cnt; |
325 | ++cp; | |
326 | } | |
dc7c9a1a WD |
327 | for (; cnt==0 && i<port_width; ++i, ++cp) { |
328 | data = (data << 8) | (*(uchar *)cp); | |
ea8015b8 WD |
329 | } |
330 | ||
dc7c9a1a | 331 | if ((rc = write_data(info, wp, SWAP(data))) != 0) { |
ea8015b8 WD |
332 | return (rc); |
333 | } | |
dc7c9a1a | 334 | wp += port_width; |
ea8015b8 WD |
335 | } |
336 | ||
337 | /* | |
338 | * handle word aligned part | |
339 | */ | |
dc7c9a1a WD |
340 | count = 0; |
341 | while (cnt >= port_width) { | |
342 | data = 0; | |
343 | for (i=0; i<port_width; ++i) { | |
344 | data = (data << 8) | *src++; | |
345 | } | |
346 | if ((rc = write_data(info, wp, SWAP(data))) != 0) { | |
ea8015b8 WD |
347 | return (rc); |
348 | } | |
dc7c9a1a WD |
349 | wp += port_width; |
350 | cnt -= port_width; | |
351 | if (count++ > 0x800) | |
352 | { | |
8bde7f77 | 353 | spin_wheel(); |
dc7c9a1a WD |
354 | count = 0; |
355 | } | |
ea8015b8 WD |
356 | } |
357 | ||
358 | if (cnt == 0) { | |
dc7c9a1a | 359 | return (0); |
ea8015b8 WD |
360 | } |
361 | ||
362 | /* | |
363 | * handle unaligned tail bytes | |
364 | */ | |
365 | data = 0; | |
dc7c9a1a WD |
366 | for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) { |
367 | data = (data << 8) | *src++; | |
ea8015b8 WD |
368 | --cnt; |
369 | } | |
dc7c9a1a WD |
370 | for (; i<port_width; ++i, ++cp) { |
371 | data = (data << 8) | (*(uchar *)cp); | |
372 | } | |
373 | ||
374 | return (write_data(info, wp, SWAP(data))); | |
375 | } | |
376 | ||
377 | /*----------------------------------------------------------------------- | |
378 | * Write a word or halfword to Flash, returns: | |
379 | * 0 - OK | |
380 | * 1 - write timeout | |
381 | * 2 - Flash not erased | |
382 | */ | |
383 | static int write_data (flash_info_t *info, ulong dest, FPW data) | |
384 | { | |
385 | FPWV *addr = (FPWV *)dest; | |
386 | ulong status; | |
387 | int flag; | |
388 | ||
389 | /* Check if Flash is (sufficiently) erased */ | |
390 | if ((*addr & data) != data) { | |
391 | printf("not erased at %08lx (%x)\n",(ulong)addr,*addr); | |
392 | return (2); | |
393 | } | |
394 | /* Disable interrupts which might cause a timeout here */ | |
395 | flag = disable_interrupts(); | |
396 | ||
397 | *addr = (FPW)0x00400040; /* write setup */ | |
398 | *addr = data; | |
399 | ||
400 | /* arm simple, non interrupt dependent timer */ | |
401 | reset_timer_masked(); | |
402 | ||
403 | /* wait while polling the status register */ | |
404 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { | |
405 | if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) { | |
406 | *addr = (FPW)0x00FF00FF; /* restore read mode */ | |
407 | return (1); | |
408 | } | |
ea8015b8 WD |
409 | } |
410 | ||
dc7c9a1a WD |
411 | *addr = (FPW)0x00FF00FF; /* restore read mode */ |
412 | ||
413 | return (0); | |
414 | } | |
415 | ||
416 | void inline | |
417 | spin_wheel(void) | |
418 | { | |
419 | static int p=0; | |
420 | static char w[] = "\\/-"; | |
421 | ||
422 | printf("\010%c", w[p]); | |
423 | (++p == 3) ? (p = 0) : 0; | |
ea8015b8 | 424 | } |