3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
12 #if defined(CONFIG_ENV_IS_IN_FLASH)
13 # ifndef CONFIG_ENV_ADDR
14 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
16 # ifndef CONFIG_ENV_SIZE
17 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
19 # ifndef CONFIG_ENV_SECT_SIZE
20 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
25 /*---------------------------------------------------------------------*/
29 #define DEBUGF(fmt,args...) printf(fmt ,##args)
31 #define DEBUGF(fmt,args...)
33 /*---------------------------------------------------------------------*/
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
37 /*-----------------------------------------------------------------------
40 static ulong flash_get_size (vu_char *addr, flash_info_t *info);
41 static int write_data (flash_info_t *info, uchar *dest, uchar data);
42 static void flash_get_offsets (ulong base, flash_info_t *info);
45 #define BYTEME(x) ((x) & 0xFF)
47 /*-----------------------------------------------------------------------
50 unsigned long flash_init (void)
52 unsigned long flash_banks[CONFIG_SYS_MAX_FLASH_BANKS] = CONFIG_SYS_FLASH_BANKS;
53 unsigned long size, size_b[CONFIG_SYS_MAX_FLASH_BANKS];
57 /* Init: no FLASHes known */
58 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
60 flash_info[i].flash_id = FLASH_UNKNOWN;
62 DEBUGF("Get flash bank %d @ 0x%08lx\n", i, flash_banks[i]);
64 size_b[i] = flash_get_size((vu_char *)flash_banks[i], &flash_info[i]);
66 size_b[i] = flash_get_size((vu_char *) 0xff800000 , &flash_info[i]);
68 if (flash_info[i].flash_id == FLASH_UNKNOWN)
70 printf ("## Unknown FLASH on Bank %d: "
71 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
72 i, flash_info[i].flash_id,
73 size_b[i], size_b[i]<<20);
77 DEBUGF("## Flash bank %d at 0x%08lx sizes: 0x%08lx \n",
78 i, flash_banks[i], size_b[i]);
80 flash_get_offsets (flash_banks[i], &flash_info[i]);
81 flash_info[i].size = size_b[i];
86 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
87 DEBUGF("protect monitor %x @ %x\n", CONFIG_SYS_MONITOR_BASE, CONFIG_SYS_MONITOR_LEN);
88 /* monitor protection ON by default */
89 flash_protect(FLAG_PROTECT_SET,
90 CONFIG_SYS_MONITOR_BASE,
91 CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1,
95 #ifdef CONFIG_ENV_IS_IN_FLASH
96 /* ENV protection ON by default */
97 DEBUGF("protect environtment %x @ %x\n", CONFIG_ENV_ADDR, CONFIG_ENV_SECT_SIZE);
98 flash_protect(FLAG_PROTECT_SET,
100 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
105 DEBUGF("## Final Flash bank sizes: ");
106 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
108 DEBUGF("%08lx ", size_b[i]);
115 /*-----------------------------------------------------------------------
117 static void flash_get_offsets (ulong base, flash_info_t *info)
121 if (info->flash_id == FLASH_UNKNOWN) {
125 switch (info->flash_id & FLASH_VENDMASK) {
126 case FLASH_MAN_INTEL:
127 for (i = 0; i < info->sector_count; i++) {
128 info->start[i] = base;
129 base += 0x00020000; /* 128k per bank */
134 printf ("Don't know sector ofsets for flash type 0x%lx\n", info->flash_id);
139 /*-----------------------------------------------------------------------
141 void flash_print_info (flash_info_t *info)
145 if (info->flash_id == FLASH_UNKNOWN) {
146 printf ("missing or unknown FLASH type\n");
150 switch (info->flash_id & FLASH_VENDMASK) {
151 case FLASH_MAN_AMD: printf ("AMD "); break;
152 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
153 case FLASH_MAN_SST: printf ("SST "); break;
154 case FLASH_MAN_STM: printf ("STM "); break;
155 case FLASH_MAN_INTEL: printf ("Intel "); break;
156 case FLASH_MAN_MT: printf ("MT "); break;
157 default: printf ("Unknown Vendor "); break;
160 switch (info->flash_id & FLASH_TYPEMASK) {
161 case FLASH_28F320J3A:
162 printf ("28F320J3A (32Mbit = 128K x 32)\n");
164 case FLASH_28F640J3A:
165 printf ("28F640J3A (64Mbit = 128K x 64)\n");
167 case FLASH_28F128J3A:
168 printf ("28F128J3A (128Mbit = 128K x 128)\n");
171 printf ("Unknown Chip Type\n");
176 if (info->size >= (1 << 20)) {
181 printf (" Size: %ld %cB in %d Sectors\n",
183 (i == 20) ? 'M' : 'k',
186 printf (" Sector Start Addresses:");
187 for (i=0; i<info->sector_count; ++i) {
192 info->protect[i] ? " (RO)" : " "
200 /*-----------------------------------------------------------------------
204 /*-----------------------------------------------------------------------
208 * The following code cannot be run from FLASH!
210 static ulong flash_get_size (vu_char *addr, flash_info_t *info)
212 vu_char manuf, device;
216 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, manuf);
219 case BYTEME(AMD_MANUFACT):
220 info->flash_id = FLASH_MAN_AMD;
222 case BYTEME(FUJ_MANUFACT):
223 info->flash_id = FLASH_MAN_FUJ;
225 case BYTEME(SST_MANUFACT):
226 info->flash_id = FLASH_MAN_SST;
228 case BYTEME(STM_MANUFACT):
229 info->flash_id = FLASH_MAN_STM;
231 case BYTEME(INTEL_MANUFACT):
232 info->flash_id = FLASH_MAN_INTEL;
235 info->flash_id = FLASH_UNKNOWN;
236 info->sector_count = 0;
238 addr[0] = BS(0xFF); /* restore read mode, (yes, BS is a NOP) */
239 return 0; /* no or unknown flash */
242 device = BS(addr[2]); /* device ID */
244 DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", (ulong)(&addr[1]), device);
247 case BYTEME(INTEL_ID_28F320J3A):
248 info->flash_id += FLASH_28F320J3A;
249 info->sector_count = 32;
250 info->size = 0x00400000;
253 case BYTEME(INTEL_ID_28F640J3A):
254 info->flash_id += FLASH_28F640J3A;
255 info->sector_count = 64;
256 info->size = 0x00800000;
259 case BYTEME(INTEL_ID_28F128J3A):
260 info->flash_id += FLASH_28F128J3A;
261 info->sector_count = 128;
262 info->size = 0x01000000;
263 break; /* => 16 MB */
266 info->flash_id = FLASH_UNKNOWN;
267 addr[0] = BS(0xFF); /* restore read mode (yes, a NOP) */
268 return 0; /* => no or unknown flash */
272 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
273 printf ("** ERROR: sector count %d > max (%d) **\n",
274 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
275 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
278 addr[0] = BS(0xFF); /* restore read mode */
284 /*-----------------------------------------------------------------------
287 int flash_erase (flash_info_t *info, int s_first, int s_last)
289 int flag, prot, sect;
290 ulong start, now, last;
292 if ((s_first < 0) || (s_first > s_last)) {
293 if (info->flash_id == FLASH_UNKNOWN) {
294 printf ("- missing\n");
296 printf ("- no sectors to erase\n");
301 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
302 printf ("Can erase only Intel flash types - aborted\n");
307 for (sect=s_first; sect<=s_last; ++sect) {
308 if (info->protect[sect]) {
314 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
319 start = get_timer (0);
321 /* Start erase on unprotected sectors */
322 for (sect = s_first; sect<=s_last; sect++) {
323 if (info->protect[sect] == 0) { /* not protected */
324 vu_char *addr = (vu_char *)(info->start[sect]);
325 unsigned long status;
327 /* Disable interrupts which might cause a timeout here */
328 flag = disable_interrupts();
330 *addr = BS(0x50); /* clear status register */
331 *addr = BS(0x20); /* erase setup */
332 *addr = BS(0xD0); /* erase confirm */
334 /* re-enable interrupts if necessary */
339 /* wait at least 80us - let's wait 1 ms */
342 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
343 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
344 printf ("Timeout\n");
345 *addr = BS(0xB0); /* suspend erase */
346 *addr = BS(0xFF); /* reset to read mode */
350 /* show that we're waiting */
351 if ((now - last) > 1000) { /* every second */
357 *addr = BS(0xFF); /* reset to read mode */
364 /*-----------------------------------------------------------------------
365 * Copy memory to flash, returns:
368 * 2 - Flash not erased
369 * 4 - Flash not identified
372 #define FLASH_WIDTH 1 /* flash bus width in bytes */
374 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
376 uchar *wp = (uchar *)addr;
379 if (info->flash_id == FLASH_UNKNOWN) {
384 if ((rc = write_data(info, wp, *src)) != 0) {
395 /*-----------------------------------------------------------------------
396 * Write a word to Flash, returns:
399 * 2 - Flash not erased
401 static int write_data (flash_info_t *info, uchar *dest, uchar data)
403 vu_char *addr = (vu_char *)dest;
408 /* Check if Flash is (sufficiently) erased */
409 if ((BS(*addr) & data) != data) {
412 /* Disable interrupts which might cause a timeout here */
413 flag = disable_interrupts();
415 *addr = BS(0x40); /* write setup */
418 /* re-enable interrupts if necessary */
423 start = get_timer (0);
425 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
426 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
427 *addr = BS(0xFF); /* restore read mode */
432 *addr = BS(0xFF); /* restore read mode */
437 /*-----------------------------------------------------------------------