3 * Yusdi Santoso, Adaptec Inc., yusdi_santoso@adaptec.com
5 * (C) Copyright 2000-2005
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/processor.h>
14 #include <asm/pci_io.h>
17 #define ROM_CS0_START 0xFF800000
18 #define ROM_CS1_START 0xFF000000
20 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
22 #if defined(CONFIG_ENV_IS_IN_FLASH)
23 # ifndef CONFIG_ENV_ADDR
24 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
26 # ifndef CONFIG_ENV_SIZE
27 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
29 # ifndef CONFIG_ENV_SECT_SIZE
30 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
34 /*-----------------------------------------------------------------------
37 static int write_word (flash_info_t *info, ulong dest, ulong data);
39 /*flash command address offsets*/
45 #define FLASH_WORD_SIZE unsigned char
47 /*-----------------------------------------------------------------------
50 static unsigned long flash_id (unsigned char mfct, unsigned char chip)
51 __attribute__ ((const));
54 FLASH_WORD_SIZE extval;
55 unsigned short intval;
58 static unsigned long flash_id (unsigned char mfct, unsigned char chip)
60 static const map_entry mfct_map[] = {
61 {(FLASH_WORD_SIZE) AMD_MANUFACT,
62 (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
63 {(FLASH_WORD_SIZE) FUJ_MANUFACT,
64 (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
65 {(FLASH_WORD_SIZE) STM_MANUFACT,
66 (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
67 {(FLASH_WORD_SIZE) MT_MANUFACT,
68 (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
69 {(FLASH_WORD_SIZE) INTEL_MANUFACT,
70 (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
71 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,
72 (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
75 static const map_entry chip_map[] = {
76 {AMD_ID_F040B, FLASH_AM040},
77 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
81 unsigned long result = FLASH_UNKNOWN;
84 for (p = &chip_map[0];
85 p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
86 if (p->extval == chip) {
87 result = FLASH_VENDMASK | p->intval;
92 for (p = &mfct_map[0];
93 p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
94 if (p->extval == mfct) {
95 result &= ~FLASH_VENDMASK;
96 result |= (unsigned long) p->intval << 16;
103 unsigned long flash_init (void)
107 static const ulong flash_banks[] = CONFIG_SYS_FLASH_BANKS;
109 /* Init: no FLASHes known */
110 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
111 flash_info_t *const pflinfo = &flash_info[i];
113 pflinfo->flash_id = FLASH_UNKNOWN;
115 pflinfo->sector_count = 0;
118 /* Enable writes to Hidden Dragon flash */
120 register unsigned char temp;
122 CONFIG_READ_BYTE (CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR,
124 temp &= ~0x20; /* clear BIOSWP bit */
125 CONFIG_WRITE_BYTE (CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR,
129 for (i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++) {
130 flash_info_t *const pflinfo = &flash_info[i];
131 const unsigned long base_address = flash_banks[i];
132 volatile FLASH_WORD_SIZE *const flash =
133 (FLASH_WORD_SIZE *) base_address;
135 flash[0xAAA << (3 * i)] = 0xaa;
136 flash[0x555 << (3 * i)] = 0x55;
137 flash[0xAAA << (3 * i)] = 0x90;
138 __asm__ __volatile__ ("sync");
141 flash_id (flash[0x0], flash[0x2 + 14 * i]);
143 switch (pflinfo->flash_id & FLASH_TYPEMASK) {
145 pflinfo->size = 0x00080000;
146 pflinfo->sector_count = 8;
147 for (j = 0; j < 8; j++) {
149 base_address + 0x00010000 * j;
150 pflinfo->protect[j] = flash[(j << 16) | 0x2];
154 pflinfo->size = 0x00100000;
155 pflinfo->sector_count = 19;
156 pflinfo->start[0] = base_address;
157 pflinfo->start[1] = base_address + 0x4000;
158 pflinfo->start[2] = base_address + 0x6000;
159 pflinfo->start[3] = base_address + 0x8000;
160 for (j = 1; j < 16; j++) {
161 pflinfo->start[j + 3] =
162 base_address + 0x00010000 * j;
166 /* The chip used is not listed in flash_id
167 TODO: Change this to explicitly detect the flash type
170 int sector_addr = base_address;
172 pflinfo->size = 0x00200000;
173 pflinfo->sector_count = 35;
174 pflinfo->start[0] = sector_addr;
175 sector_addr += 0x4000; /* 16K */
176 pflinfo->start[1] = sector_addr;
177 sector_addr += 0x2000; /* 8K */
178 pflinfo->start[2] = sector_addr;
179 sector_addr += 0x2000; /* 8K */
180 pflinfo->start[3] = sector_addr;
181 sector_addr += 0x8000; /* 32K */
183 for (j = 4; j < 35; j++) {
184 pflinfo->start[j] = sector_addr;
185 sector_addr += 0x10000; /* 64K */
190 /* Protect monitor and environment sectors
192 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
193 flash_protect (FLAG_PROTECT_SET,
194 CONFIG_SYS_MONITOR_BASE,
195 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
199 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
200 flash_protect (FLAG_PROTECT_SET,
202 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
206 /* reset device to read mode */
207 flash[0x0000] = 0xf0;
208 __asm__ __volatile__ ("sync");
211 /* only have 1 bank */
212 return flash_info[0].size;
215 /*-----------------------------------------------------------------------
217 void flash_print_info (flash_info_t * info)
219 static const char unk[] = "Unknown";
220 const char *mfct = unk, *type = unk;
223 if (info->flash_id != FLASH_UNKNOWN) {
224 switch (info->flash_id & FLASH_VENDMASK) {
238 mfct = "Bright Microelectonics";
240 case FLASH_MAN_INTEL:
245 switch (info->flash_id & FLASH_TYPEMASK) {
247 type = "AM29F040B (512K * 8, uniform sector size)";
250 type = "AM29LV400B (4 Mbit, bottom boot sect)";
253 type = "AM29LV400T (4 Mbit, top boot sector)";
256 type = "AM29LV800B (8 Mbit, bottom boot sect)";
259 type = "AM29LV800T (8 Mbit, top boot sector)";
262 type = "AM29LV160T (16 Mbit, top boot sector)";
265 type = "AM29LV320B (32 Mbit, bottom boot sect)";
268 type = "AM29LV320T (32 Mbit, top boot sector)";
271 type = "M29W800AB (8 Mbit, bottom boot sect)";
274 type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
277 type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
282 printf ("\n Brand: %s Type: %s\n"
283 " Size: %lu KB in %d Sectors\n",
284 mfct, type, info->size >> 10, info->sector_count);
286 printf (" Sector Start Addresses:");
288 for (i = 0; i < info->sector_count; i++) {
291 unsigned long *flash = (unsigned long *) info->start[i];
294 * Check if whole sector is erased
296 size = (i != (info->sector_count - 1)) ?
297 (info->start[i + 1] - info->start[i]) >> 2 :
298 (info->start[0] + info->size - info->start[i]) >> 2;
300 for (flash = (unsigned long *) info->start[i], erased = 1;
301 (flash != (unsigned long *) info->start[i] + size)
303 erased = *flash == ~0x0UL;
305 printf ("%s %08lX %s %s",
306 (i % 5) ? "" : "\n ",
308 erased ? "E" : " ", info->protect[i] ? "RO" : " ");
315 int flash_erase (flash_info_t * info, int s_first, int s_last)
317 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
318 int flag, prot, sect, l_sect;
319 ulong start, now, last;
322 if ((s_first < 0) || (s_first > s_last)) {
323 if (info->flash_id == FLASH_UNKNOWN) {
324 printf ("- missing\n");
326 printf ("- no sectors to erase\n");
331 if ((info->flash_id == FLASH_UNKNOWN) ||
332 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
333 printf ("Can't erase unknown flash type - aborted\n");
338 for (sect = s_first; sect <= s_last; ++sect) {
339 if (info->protect[sect]) {
345 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
352 /* Check the ROM CS */
353 if ((info->start[0] >= ROM_CS1_START)
354 && (info->start[0] < ROM_CS0_START))
359 /* Disable interrupts which might cause a timeout here */
360 flag = disable_interrupts ();
362 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
363 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
364 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
365 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
366 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
368 /* Start erase on unprotected sectors */
369 for (sect = s_first; sect <= s_last; sect++) {
370 if (info->protect[sect] == 0) { /* not protected */
371 addr = (FLASH_WORD_SIZE *) (info->start[0] +
372 ((info->start[sect] -
373 info->start[0]) << sh8b));
374 if (info->flash_id & FLASH_MAN_SST) {
375 addr[ADDR0 << sh8b] =
376 (FLASH_WORD_SIZE) 0x00AA00AA;
377 addr[ADDR1 << sh8b] =
378 (FLASH_WORD_SIZE) 0x00550055;
379 addr[ADDR0 << sh8b] =
380 (FLASH_WORD_SIZE) 0x00800080;
381 addr[ADDR0 << sh8b] =
382 (FLASH_WORD_SIZE) 0x00AA00AA;
383 addr[ADDR1 << sh8b] =
384 (FLASH_WORD_SIZE) 0x00550055;
385 addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
386 udelay (30000); /* wait 30 ms */
388 addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
393 /* re-enable interrupts if necessary */
395 enable_interrupts ();
397 /* wait at least 80us - let's wait 1 ms */
401 * We wait for the last triggered sector
406 start = get_timer (0);
408 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] -
411 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
412 (FLASH_WORD_SIZE) 0x00800080) {
413 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
414 printf ("Timeout\n");
417 /* show that we're waiting */
418 if ((now - last) > 1000) { /* every second */
425 /* reset to read mode */
426 addr = (FLASH_WORD_SIZE *) info->start[0];
427 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
433 /*-----------------------------------------------------------------------
434 * Copy memory to flash, returns:
437 * 2 - Flash not erased
440 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
445 wp = (addr & ~3); /* get lower word aligned address */
448 * handle unaligned start bytes
450 if ((l = addr - wp) != 0) {
452 for (i = 0, cp = wp; i < l; ++i, ++cp) {
453 data = (data << 8) | (*(uchar *) cp);
455 for (; i < 4 && cnt > 0; ++i) {
456 data = (data << 8) | *src++;
460 for (; cnt == 0 && i < 4; ++i, ++cp) {
461 data = (data << 8) | (*(uchar *) cp);
464 if ((rc = write_word (info, wp, data)) != 0) {
471 * handle word aligned part
475 for (i = 0; i < 4; ++i) {
476 data = (data << 8) | *src++;
478 if ((rc = write_word (info, wp, data)) != 0) {
490 * handle unaligned tail bytes
493 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
494 data = (data << 8) | *src++;
497 for (; i < 4; ++i, ++cp) {
498 data = (data << 8) | (*(uchar *) cp);
501 return (write_word (info, wp, data));
504 /*-----------------------------------------------------------------------
505 * Write a word to Flash, returns:
508 * 2 - Flash not erased
510 static int write_word (flash_info_t * info, ulong dest, ulong data)
512 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0];
513 volatile FLASH_WORD_SIZE *dest2;
514 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
520 /* Check the ROM CS */
521 if ((info->start[0] >= ROM_CS1_START)
522 && (info->start[0] < ROM_CS0_START))
527 dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
530 /* Check if Flash is (sufficiently) erased */
531 if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
534 /* Disable interrupts which might cause a timeout here */
535 flag = disable_interrupts ();
537 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
538 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
539 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
540 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0;
542 dest2[i << sh8b] = data2[i];
544 /* re-enable interrupts if necessary */
546 enable_interrupts ();
548 /* data polling for D7 */
549 start = get_timer (0);
550 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) !=
551 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
552 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {