]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/utx8245/flash.c
mpc8260: remove atc board support
[people/ms/u-boot.git] / board / utx8245 / flash.c
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2002
6 * Gregory E. Allen, gallen@arlut.utexas.edu
7 * Matthew E. Karger, karger@arlut.utexas.edu
8 * Applied Research Laboratories, The University of Texas at Austin
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13 #include <common.h>
14 #include <mpc824x.h>
15 #include <asm/processor.h>
16
17 #define ROM_CS0_START 0xFF800000
18 #define ROM_CS1_START 0xFF000000
19
20 #if defined(CONFIG_ENV_IS_IN_FLASH)
21 # ifndef CONFIG_ENV_ADDR
22 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
23 # endif
24 # ifndef CONFIG_ENV_SIZE
25 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
26 # endif
27 # ifndef CONFIG_ENV_SECT_SIZE
28 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
29 # endif
30 #endif
31
32 #define FLASH_BANK_SIZE ((uint)(16 * 1024 * 1024)) /* max 16Mbyte */
33 #define MAIN_SECT_SIZE 0x10000
34 #define SECT_SIZE_32KB 0x8000
35 #define SECT_SIZE_8KB 0x2000
36
37 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
38
39 static int write_word (flash_info_t * info, ulong dest, ulong data);
40 #if 0
41 static void write_via_fpu (vu_long * addr, ulong * data);
42 #endif
43 static __inline__ unsigned long get_msr (void);
44 static __inline__ void set_msr (unsigned long msr);
45
46 /*flash command address offsets*/
47 #define ADDR0 (0x555)
48 #define ADDR1 (0xAAA)
49 #define ADDR3 (0x001)
50
51 #define FLASH_WORD_SIZE unsigned char
52
53 /*---------------------------------------------------------------------*/
54 /*#define DEBUG_FLASH 1 */
55
56 /*---------------------------------------------------------------------*/
57
58 unsigned long flash_init (void)
59 {
60 int i; /* flash bank counter */
61 int j; /* flash device sector counter */
62 int k; /* flash size calculation loop counter */
63 int N; /* pow(2,N) is flash size, but we don't have <math.h> */
64 ulong total_size = 0, device_size = 1;
65 unsigned char manuf_id, device_id;
66
67 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
68 vu_char *addr = (vu_char *) (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE);
69
70 addr[0x555] = 0xAA; /* get manuf/device info command */
71 addr[0x2AA] = 0x55; /* 3-cycle command */
72 addr[0x555] = 0x90;
73
74 manuf_id = addr[0]; /* read back manuf/device info */
75 device_id = addr[1];
76
77 addr[0x55] = 0x98; /* CFI command */
78 N = addr[0x27]; /* read back device_size = pow(2,N) */
79
80 for (k = 0; k < N; k++) /* calculate device_size = pow(2,N) */
81 device_size *= 2;
82
83 flash_info[i].size = device_size;
84 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
85
86 #if defined DEBUG_FLASH
87 printf ("manuf_id = %x, device_id = %x\n", manuf_id, device_id);
88 #endif
89 /* find out what kind of flash we are using */
90 if ((manuf_id == (uchar) (AMD_MANUFACT))
91 && (device_id == AMD_ID_LV033C)) {
92 flash_info[i].flash_id =
93 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
94 (FLASH_AM033C & FLASH_TYPEMASK);
95
96 /* set individual sector start addresses */
97 for (j = 0; j < flash_info[i].sector_count; j++) {
98 flash_info[i].start[j] =
99 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
100 j * MAIN_SECT_SIZE);
101 }
102 }
103
104 else if ((manuf_id == (uchar) (AMD_MANUFACT)) &&
105 (device_id == AMD_ID_LV116DT)) {
106 flash_info[i].flash_id =
107 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
108 (FLASH_AM160T & FLASH_TYPEMASK);
109
110 /* set individual sector start addresses */
111 for (j = 0; j < flash_info[i].sector_count; j++) {
112 flash_info[i].start[j] =
113 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
114 j * MAIN_SECT_SIZE);
115
116 if (j < (CONFIG_SYS_MAX_FLASH_SECT - 3)) {
117 flash_info[i].start[j] =
118 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
119 j * MAIN_SECT_SIZE);
120 } else if (j == (CONFIG_SYS_MAX_FLASH_SECT - 3)) {
121 flash_info[i].start[j] =
122 (flash_info[i].start[j - 1] + SECT_SIZE_32KB);
123
124 } else {
125 flash_info[i].start[j] =
126 (flash_info[i].start[j - 1] + SECT_SIZE_8KB);
127 }
128 }
129 }
130
131 else {
132 flash_info[i].flash_id = FLASH_UNKNOWN;
133 addr[0] = 0xFF;
134 goto Done;
135 }
136
137 #if defined DEBUG_FLASH
138 printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
139 #endif
140
141 addr[0] = 0xFF;
142
143 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
144
145 total_size += flash_info[i].size;
146 }
147
148 /* Protect monitor and environment sectors
149 */
150 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
151 flash_protect (FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE,
152 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
153 &flash_info[0]);
154 #endif
155
156 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
157 flash_protect (FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
158 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
159 #endif
160
161 Done:
162 return total_size;
163 }
164
165 /*-----------------------------------------------------------------------
166 */
167 void flash_print_info (flash_info_t * info)
168 {
169 static const char unk[] = "Unknown";
170 const char *mfct = unk, *type = unk;
171 unsigned int i;
172
173 if (info->flash_id != FLASH_UNKNOWN) {
174 switch (info->flash_id & FLASH_VENDMASK) {
175 case FLASH_MAN_AMD:
176 mfct = "AMD";
177 break;
178 case FLASH_MAN_FUJ:
179 mfct = "FUJITSU";
180 break;
181 case FLASH_MAN_STM:
182 mfct = "STM";
183 break;
184 case FLASH_MAN_SST:
185 mfct = "SST";
186 break;
187 case FLASH_MAN_BM:
188 mfct = "Bright Microelectonics";
189 break;
190 case FLASH_MAN_INTEL:
191 mfct = "Intel";
192 break;
193 }
194
195 switch (info->flash_id & FLASH_TYPEMASK) {
196 case FLASH_AM033C:
197 type = "AM29LV033C (32 Mbit, uniform sector size)";
198 break;
199 case FLASH_AM160T:
200 type = "AM29LV160T (16 Mbit, top boot sector)";
201 break;
202 case FLASH_AM040:
203 type = "AM29F040B (512K * 8, uniform sector size)";
204 break;
205 case FLASH_AM400B:
206 type = "AM29LV400B (4 Mbit, bottom boot sect)";
207 break;
208 case FLASH_AM400T:
209 type = "AM29LV400T (4 Mbit, top boot sector)";
210 break;
211 case FLASH_AM800B:
212 type = "AM29LV800B (8 Mbit, bottom boot sect)";
213 break;
214 case FLASH_AM800T:
215 type = "AM29LV800T (8 Mbit, top boot sector)";
216 break;
217 case FLASH_AM320B:
218 type = "AM29LV320B (32 Mbit, bottom boot sect)";
219 break;
220 case FLASH_AM320T:
221 type = "AM29LV320T (32 Mbit, top boot sector)";
222 break;
223 case FLASH_STM800AB:
224 type = "M29W800AB (8 Mbit, bottom boot sect)";
225 break;
226 case FLASH_SST800A:
227 type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
228 break;
229 case FLASH_SST160A:
230 type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
231 break;
232 }
233 }
234
235 printf ("\n Brand: %s Type: %s\n"
236 " Size: %lu KB in %d Sectors\n",
237 mfct, type, info->size >> 10, info->sector_count);
238
239 printf (" Sector Start Addresses:");
240
241 for (i = 0; i < info->sector_count; i++) {
242 unsigned long size;
243 unsigned int erased;
244 unsigned long *flash = (unsigned long *) info->start[i];
245
246 /*
247 * Check if whole sector is erased
248 */
249 size = (i != (info->sector_count - 1)) ?
250 (info->start[i + 1] - info->start[i]) >> 2 :
251 (info->start[0] + info->size - info->start[i]) >> 2;
252
253 for (flash = (unsigned long *) info->start[i], erased = 1;
254 (flash != (unsigned long *) info->start[i] + size) && erased;
255 flash++)
256 erased = *flash == ~0x0UL;
257
258 printf ("%s %08lX %s %s",
259 (i % 5) ? "" : "\n ",
260 info->start[i],
261 erased ? "E" : " ", info->protect[i] ? "RO" : " ");
262 }
263
264 puts ("\n");
265 return;
266 }
267
268 /*-----------------------------------------------------------------------
269 */
270
271 int flash_erase (flash_info_t * info, int s_first, int s_last)
272 {
273 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
274 int flag, prot, sect, l_sect;
275 ulong start, now, last;
276 unsigned char sh8b;
277
278 if ((s_first < 0) || (s_first > s_last)) {
279 if (info->flash_id == FLASH_UNKNOWN) {
280 printf ("- missing\n");
281 } else {
282 printf ("- no sectors to erase\n");
283 }
284 return 1;
285 }
286
287 if ((info->flash_id == FLASH_UNKNOWN) ||
288 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
289 printf ("Can't erase unknown flash type - aborted\n");
290 return 1;
291 }
292
293 prot = 0;
294 for (sect = s_first; sect <= s_last; ++sect) {
295 if (info->protect[sect]) {
296 prot++;
297 }
298 }
299
300 if (prot) {
301 printf ("- Warning: %d protected sectors will not be erased!\n",
302 prot);
303 } else {
304 printf ("\n");
305 }
306
307 l_sect = -1;
308
309 /* Check the ROM CS */
310 if ((info->start[0] >= ROM_CS1_START)
311 && (info->start[0] < ROM_CS0_START))
312 sh8b = 3;
313 else
314 sh8b = 0;
315
316 /* Disable interrupts which might cause a timeout here */
317 flag = disable_interrupts ();
318
319 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
320 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
321 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
322 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
323 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
324
325 /* Start erase on unprotected sectors */
326 for (sect = s_first; sect <= s_last; sect++) {
327 if (info->protect[sect] == 0) { /* not protected */
328 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->
329 start[sect] -
330 info->
331 start[0]) <<
332 sh8b));
333
334 if (info->flash_id & FLASH_MAN_SST) {
335 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
336 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
337 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
338 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
339 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
340 addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
341 udelay (30000); /* wait 30 ms */
342 } else {
343 addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
344 }
345
346 l_sect = sect;
347 }
348 }
349
350 /* re-enable interrupts if necessary */
351 if (flag)
352 enable_interrupts ();
353
354 /* wait at least 80us - let's wait 1 ms */
355 udelay (1000);
356
357 /*
358 * We wait for the last triggered sector
359 */
360 if (l_sect < 0)
361 goto DONE;
362
363 start = get_timer (0);
364 last = start;
365 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] -
366 info->
367 start[0]) << sh8b));
368 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
369 (FLASH_WORD_SIZE) 0x00800080) {
370 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
371 printf ("Timeout\n");
372 return 1;
373 }
374 /* show that we're waiting */
375 if ((now - last) > 1000) { /* every second */
376 serial_putc ('.');
377 last = now;
378 }
379 }
380
381 DONE:
382 /* reset to read mode */
383 addr = (FLASH_WORD_SIZE *) info->start[0];
384 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
385
386 printf (" done\n");
387 return 0;
388 }
389
390
391 /*-----------------------------------------------------------------------
392 * Copy memory to flash, returns:
393 * 0 - OK
394 * 1 - write timeout
395 * 2 - Flash not erased
396 */
397
398 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
399 {
400 ulong cp, wp, data;
401 int i, l, rc;
402
403 wp = (addr & ~3); /* get lower word aligned address */
404
405 /*
406 * handle unaligned start bytes
407 */
408 if ((l = addr - wp) != 0) {
409 data = 0;
410 for (i = 0, cp = wp; i < l; ++i, ++cp) {
411 data = (data << 8) | (*(uchar *) cp);
412 }
413 for (; i < 4 && cnt > 0; ++i) {
414 data = (data << 8) | *src++;
415 --cnt;
416 ++cp;
417 }
418 for (; cnt == 0 && i < 4; ++i, ++cp) {
419 data = (data << 8) | (*(uchar *) cp);
420 }
421
422 if ((rc = write_word (info, wp, data)) != 0) {
423 return (rc);
424 }
425 wp += 4;
426 }
427
428 /*
429 * handle word aligned part
430 */
431 while (cnt >= 4) {
432 data = 0;
433 for (i = 0; i < 4; ++i) {
434 data = (data << 8) | *src++;
435 }
436 if ((rc = write_word (info, wp, data)) != 0) {
437 return (rc);
438 }
439 wp += 4;
440 cnt -= 4;
441 }
442
443 if (cnt == 0) {
444 return (0);
445 }
446
447 /*
448 * handle unaligned tail bytes
449 */
450 data = 0;
451 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
452 data = (data << 8) | *src++;
453 --cnt;
454 }
455 for (; i < 4; ++i, ++cp) {
456 data = (data << 8) | (*(uchar *) cp);
457 }
458
459 return (write_word (info, wp, data));
460 }
461
462
463 /*-----------------------------------------------------------------------
464 * Write a word to Flash, returns:
465 * 0 - OK
466 * 1 - write timeout
467 * 2 - Flash not erased
468 */
469 static int write_word (flash_info_t * info, ulong dest, ulong data)
470 {
471 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0];
472 volatile FLASH_WORD_SIZE *dest2;
473 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
474 ulong start;
475 int flag;
476 int i;
477 unsigned char sh8b;
478
479 /* Check the ROM CS */
480 if ((info->start[0] >= ROM_CS1_START)
481 && (info->start[0] < ROM_CS0_START))
482 sh8b = 3;
483 else
484 sh8b = 0;
485
486 dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
487 info->start[0]);
488
489 /* Check if Flash is (sufficiently) erased */
490 if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
491 return (2);
492 }
493 /* Disable interrupts which might cause a timeout here */
494 flag = disable_interrupts ();
495
496 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
497 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
498 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
499 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0;
500
501 dest2[i << sh8b] = data2[i];
502
503 /* re-enable interrupts if necessary */
504 if (flag)
505 enable_interrupts ();
506
507 /* data polling for D7 */
508 start = get_timer (0);
509 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) !=
510 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
511 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
512 return (1);
513 }
514 }
515 }
516
517 return (0);
518 }
519
520 /*-----------------------------------------------------------------------
521 */
522 #if 0
523 static void write_via_fpu (vu_long * addr, ulong * data)
524 {
525 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
526 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
527 }
528 #endif
529
530 /*-----------------------------------------------------------------------
531 */
532 static __inline__ unsigned long get_msr (void)
533 {
534 unsigned long msr;
535
536 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
537
538 return msr;
539 }
540
541 static __inline__ void set_msr (unsigned long msr)
542 {
543 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));
544 }