Merge branch 'next'
[u-boot/qq2440-u-boot.git] / board / utx8245 / flash.c
blob1dfcb4151316034b1e4a49731a3ff27541ca2474
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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
10 * SPDX-License-Identifier: GPL-2.0+
13 #include <common.h>
14 #include <mpc824x.h>
15 #include <asm/processor.h>
17 #define ROM_CS0_START 0xFF800000
18 #define ROM_CS1_START 0xFF000000
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
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
37 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
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);
46 /*flash command address offsets*/
47 #define ADDR0 (0x555)
48 #define ADDR1 (0xAAA)
49 #define ADDR3 (0x001)
51 #define FLASH_WORD_SIZE unsigned char
53 /*---------------------------------------------------------------------*/
54 /*#define DEBUG_FLASH 1 */
56 /*---------------------------------------------------------------------*/
58 unsigned long flash_init (void)
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;
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);
70 addr[0x555] = 0xAA; /* get manuf/device info command */
71 addr[0x2AA] = 0x55; /* 3-cycle command */
72 addr[0x555] = 0x90;
74 manuf_id = addr[0]; /* read back manuf/device info */
75 device_id = addr[1];
77 addr[0x55] = 0x98; /* CFI command */
78 N = addr[0x27]; /* read back device_size = pow(2,N) */
80 for (k = 0; k < N; k++) /* calculate device_size = pow(2,N) */
81 device_size *= 2;
83 flash_info[i].size = device_size;
84 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
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);
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);
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);
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);
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);
124 } else {
125 flash_info[i].start[j] =
126 (flash_info[i].start[j - 1] + SECT_SIZE_8KB);
131 else {
132 flash_info[i].flash_id = FLASH_UNKNOWN;
133 addr[0] = 0xFF;
134 goto Done;
137 #if defined DEBUG_FLASH
138 printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
139 #endif
141 addr[0] = 0xFF;
143 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
145 total_size += flash_info[i].size;
148 /* Protect monitor and environment sectors
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
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
161 Done:
162 return total_size;
165 /*-----------------------------------------------------------------------
167 void flash_print_info (flash_info_t * info)
169 static const char unk[] = "Unknown";
170 const char *mfct = unk, *type = unk;
171 unsigned int i;
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;
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;
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);
239 printf (" Sector Start Addresses:");
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];
247 * Check if whole sector is erased
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;
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;
258 printf ("%s %08lX %s %s",
259 (i % 5) ? "" : "\n ",
260 info->start[i],
261 erased ? "E" : " ", info->protect[i] ? "RO" : " ");
264 puts ("\n");
265 return;
268 /*-----------------------------------------------------------------------
271 int flash_erase (flash_info_t * info, int s_first, int s_last)
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;
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");
284 return 1;
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;
293 prot = 0;
294 for (sect = s_first; sect <= s_last; ++sect) {
295 if (info->protect[sect]) {
296 prot++;
300 if (prot) {
301 printf ("- Warning: %d protected sectors will not be erased!\n",
302 prot);
303 } else {
304 printf ("\n");
307 l_sect = -1;
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;
316 /* Disable interrupts which might cause a timeout here */
317 flag = disable_interrupts ();
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;
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));
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 */
346 l_sect = sect;
350 /* re-enable interrupts if necessary */
351 if (flag)
352 enable_interrupts ();
354 /* wait at least 80us - let's wait 1 ms */
355 udelay (1000);
358 * We wait for the last triggered sector
360 if (l_sect < 0)
361 goto DONE;
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;
374 /* show that we're waiting */
375 if ((now - last) > 1000) { /* every second */
376 serial_putc ('.');
377 last = now;
381 DONE:
382 /* reset to read mode */
383 addr = (FLASH_WORD_SIZE *) info->start[0];
384 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
386 printf (" done\n");
387 return 0;
391 /*-----------------------------------------------------------------------
392 * Copy memory to flash, returns:
393 * 0 - OK
394 * 1 - write timeout
395 * 2 - Flash not erased
398 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
400 ulong cp, wp, data;
401 int i, l, rc;
403 wp = (addr & ~3); /* get lower word aligned address */
406 * handle unaligned start bytes
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);
413 for (; i < 4 && cnt > 0; ++i) {
414 data = (data << 8) | *src++;
415 --cnt;
416 ++cp;
418 for (; cnt == 0 && i < 4; ++i, ++cp) {
419 data = (data << 8) | (*(uchar *) cp);
422 if ((rc = write_word (info, wp, data)) != 0) {
423 return (rc);
425 wp += 4;
429 * handle word aligned part
431 while (cnt >= 4) {
432 data = 0;
433 for (i = 0; i < 4; ++i) {
434 data = (data << 8) | *src++;
436 if ((rc = write_word (info, wp, data)) != 0) {
437 return (rc);
439 wp += 4;
440 cnt -= 4;
443 if (cnt == 0) {
444 return (0);
448 * handle unaligned tail bytes
450 data = 0;
451 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
452 data = (data << 8) | *src++;
453 --cnt;
455 for (; i < 4; ++i, ++cp) {
456 data = (data << 8) | (*(uchar *) cp);
459 return (write_word (info, wp, data));
463 /*-----------------------------------------------------------------------
464 * Write a word to Flash, returns:
465 * 0 - OK
466 * 1 - write timeout
467 * 2 - Flash not erased
469 static int write_word (flash_info_t * info, ulong dest, ulong data)
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;
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;
486 dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
487 info->start[0]);
489 /* Check if Flash is (sufficiently) erased */
490 if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
491 return (2);
493 /* Disable interrupts which might cause a timeout here */
494 flag = disable_interrupts ();
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;
501 dest2[i << sh8b] = data2[i];
503 /* re-enable interrupts if necessary */
504 if (flag)
505 enable_interrupts ();
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);
517 return (0);
520 /*-----------------------------------------------------------------------
522 #if 0
523 static void write_via_fpu (vu_long * addr, ulong * data)
525 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
526 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
528 #endif
530 /*-----------------------------------------------------------------------
532 static __inline__ unsigned long get_msr (void)
534 unsigned long msr;
536 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
538 return msr;
541 static __inline__ void set_msr (unsigned long msr)
543 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));