2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 /*-----------------------------------------------------------------------
16 static ulong flash_get_size(vu_long * addr, flash_info_t * info);
17 static int write_byte(flash_info_t * info, ulong dest, uchar data);
18 static void flash_get_offsets(ulong base, flash_info_t * info);
20 /*-----------------------------------------------------------------------
23 unsigned long flash_init(void)
25 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
26 volatile memctl8xx_t *memctl = &immap->im_memctl;
30 /* Init: no FLASHes known */
31 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
32 flash_info[i].flash_id = FLASH_UNKNOWN;
34 size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
36 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
37 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
41 /* Remap FLASH according to real size */
42 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
43 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
45 /* Re-do sizing to get full correct info */
46 size = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
48 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
50 /* monitor protection ON by default */
51 flash_protect(FLAG_PROTECT_SET,
52 CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
55 flash_protect ( FLAG_PROTECT_SET,
57 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
60 #ifdef CONFIG_ENV_ADDR_REDUND
61 flash_protect ( FLAG_PROTECT_SET,
62 CONFIG_ENV_ADDR_REDUND,
63 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
67 flash_info[0].size = size;
72 /*-----------------------------------------------------------------------
74 static void flash_get_offsets(ulong base, flash_info_t * info)
78 /* set up sector start address table */
79 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
80 for (i = 0; i < info->sector_count; i++) {
81 info->start[i] = base + (i * 0x00010000);
83 } else if (info->flash_id & FLASH_BTYPE) {
84 /* set sector offsets for bottom boot block type */
85 info->start[0] = base + 0x00000000;
86 info->start[1] = base + 0x00004000;
87 info->start[2] = base + 0x00006000;
88 info->start[3] = base + 0x00008000;
89 for (i = 4; i < info->sector_count; i++) {
90 info->start[i] = base + (i * 0x00010000) - 0x00030000;
93 /* set sector offsets for top boot block type */
94 i = info->sector_count - 1;
95 info->start[i--] = base + info->size - 0x00004000;
96 info->start[i--] = base + info->size - 0x00006000;
97 info->start[i--] = base + info->size - 0x00008000;
99 info->start[i] = base + i * 0x00010000;
104 /*-----------------------------------------------------------------------
106 void flash_print_info(flash_info_t * info)
110 if (info->flash_id == FLASH_UNKNOWN) {
111 printf("missing or unknown FLASH type\n");
115 switch (info->flash_id & FLASH_VENDMASK) {
126 printf("Unknown Vendor ");
130 switch (info->flash_id & FLASH_TYPEMASK) {
132 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
135 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
138 printf("AM29LV400T (4 Mbit, top boot sector)\n");
141 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
144 printf("AM29LV800T (8 Mbit, top boot sector)\n");
147 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
150 printf("AM29LV160T (16 Mbit, top boot sector)\n");
153 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
156 printf("AM29LV320T (32 Mbit, top boot sector)\n");
159 printf("Unknown Chip Type\n");
163 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
165 printf(" Sector Start Addresses:");
166 for (i = 0; i < info->sector_count; ++i) {
169 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
174 /*-----------------------------------------------------------------------
178 /*-----------------------------------------------------------------------
182 * The following code cannot be run from FLASH!
185 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
190 vu_char *caddr = (vu_char *) addr;
191 ulong base = (ulong) addr;
193 /* Write auto select command: read Manufacturer ID */
194 caddr[0x0555] = 0xAA;
195 caddr[0x02AA] = 0x55;
196 caddr[0x0555] = 0x90;
200 case (AMD_MANUFACT & 0xFF):
201 info->flash_id = FLASH_MAN_AMD;
203 case (FUJ_MANUFACT & 0xFF):
204 info->flash_id = FLASH_MAN_FUJ;
206 case (MX_MANUFACT & 0xFF):
207 info->flash_id = FLASH_MAN_MX;
209 case (STM_MANUFACT & 0xFF):
210 info->flash_id = FLASH_MAN_STM;
213 info->flash_id = FLASH_UNKNOWN;
214 info->sector_count = 0;
216 return (0); /* no or unknown flash */
219 pid = caddr[1]; /* device ID */
221 case (AMD_ID_LV400T & 0xFF):
222 info->flash_id += FLASH_AM400T;
223 info->sector_count = 11;
224 info->size = 0x00080000;
225 break; /* => 512 kB */
227 case (AMD_ID_LV400B & 0xFF):
228 info->flash_id += FLASH_AM400B;
229 info->sector_count = 11;
230 info->size = 0x00080000;
231 break; /* => 512 kB */
233 case (AMD_ID_LV800T & 0xFF):
234 info->flash_id += FLASH_AM800T;
235 info->sector_count = 19;
236 info->size = 0x00100000;
239 case (AMD_ID_LV800B & 0xFF):
240 info->flash_id += FLASH_AM800B;
241 info->sector_count = 19;
242 info->size = 0x00100000;
245 case (AMD_ID_LV160T & 0xFF):
246 info->flash_id += FLASH_AM160T;
247 info->sector_count = 35;
248 info->size = 0x00200000;
251 case (AMD_ID_LV160B & 0xFF):
252 info->flash_id += FLASH_AM160B;
253 info->sector_count = 35;
254 info->size = 0x00200000;
257 case (AMD_ID_LV040B & 0xFF):
258 info->flash_id += FLASH_AM040;
259 info->sector_count = 8;
260 info->size = 0x00080000;
263 case (STM_ID_M29W040B & 0xFF):
264 info->flash_id += FLASH_AM040;
265 info->sector_count = 8;
266 info->size = 0x00080000;
269 #if 0 /* enable when device IDs are available */
270 case (AMD_ID_LV320T & 0xFF):
271 info->flash_id += FLASH_AM320T;
272 info->sector_count = 67;
273 info->size = 0x00400000;
276 case (AMD_ID_LV320B & 0xFF):
277 info->flash_id += FLASH_AM320B;
278 info->sector_count = 67;
279 info->size = 0x00400000;
283 info->flash_id = FLASH_UNKNOWN;
284 return (0); /* => no or unknown flash */
289 /* set up sector start address table */
290 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
291 for (i = 0; i < info->sector_count; i++) {
292 info->start[i] = base + (i * 0x00010000);
294 } else if (info->flash_id & FLASH_BTYPE) {
295 /* set sector offsets for bottom boot block type */
296 info->start[0] = base + 0x00000000;
297 info->start[1] = base + 0x00004000;
298 info->start[2] = base + 0x00006000;
299 info->start[3] = base + 0x00008000;
300 for (i = 4; i < info->sector_count; i++) {
301 info->start[i] = base + (i * 0x00010000) - 0x00030000;
304 /* set sector offsets for top boot block type */
305 i = info->sector_count - 1;
306 info->start[i--] = base + info->size - 0x00004000;
307 info->start[i--] = base + info->size - 0x00006000;
308 info->start[i--] = base + info->size - 0x00008000;
309 for (; i >= 0; i--) {
310 info->start[i] = base + i * 0x00010000;
314 /* check for protected sectors */
315 for (i = 0; i < info->sector_count; i++) {
316 /* read sector protection: D0 = 1 if protected */
317 caddr = (volatile unsigned char *)(info->start[i]);
318 info->protect[i] = caddr[2] & 1;
322 * Prevent writes to uninitialized FLASH.
324 if (info->flash_id != FLASH_UNKNOWN) {
325 caddr = (vu_char *) info->start[0];
327 caddr[0x0555] = 0xAA;
328 caddr[0x02AA] = 0x55;
329 caddr[0x0555] = 0xF0;
338 /*-----------------------------------------------------------------------
341 int flash_erase(flash_info_t * info, int s_first, int s_last)
343 vu_char *addr = (vu_char *) (info->start[0]);
344 int flag, prot, sect, l_sect;
345 ulong start, now, last;
347 if ((s_first < 0) || (s_first > s_last)) {
348 if (info->flash_id == FLASH_UNKNOWN) {
349 printf("- missing\n");
351 printf("- no sectors to erase\n");
356 if ((info->flash_id == FLASH_UNKNOWN) ||
357 (info->flash_id > FLASH_AMD_COMP)) {
358 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
363 for (sect = s_first; sect <= s_last; ++sect) {
364 if (info->protect[sect]) {
370 printf("- Warning: %d protected sectors will not be erased!\n", prot);
377 /* Disable interrupts which might cause a timeout here */
378 flag = disable_interrupts();
386 /* Start erase on unprotected sectors */
387 for (sect = s_first; sect <= s_last; sect++) {
388 if (info->protect[sect] == 0) { /* not protected */
389 addr = (vu_char *) (info->start[sect]);
395 /* re-enable interrupts if necessary */
399 /* wait at least 80us - let's wait 1 ms */
403 * We wait for the last triggered sector
408 start = get_timer(0);
410 addr = (vu_char *) (info->start[l_sect]);
411 while ((addr[0] & 0x80) != 0x80) {
412 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
416 /* show that we're waiting */
417 if ((now - last) > 1000) { /* every second */
424 /* reset to read mode */
425 addr = (vu_char *) info->start[0];
426 addr[0] = 0xF0; /* reset bank */
432 /*-----------------------------------------------------------------------
433 * Copy memory to flash, returns:
436 * 2 - Flash not erased
439 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
444 if ((rc = write_byte(info, addr++, *src++)) != 0) {
453 /*-----------------------------------------------------------------------
454 * Write a word to Flash, returns:
457 * 2 - Flash not erased
459 static int write_byte(flash_info_t * info, ulong dest, uchar data)
461 vu_char *addr = (vu_char *) (info->start[0]);
465 /* Check if Flash is (sufficiently) erased */
466 if ((*((vu_char *) dest) & data) != data) {
469 /* Disable interrupts which might cause a timeout here */
470 flag = disable_interrupts();
476 *((vu_char *) dest) = data;
478 /* re-enable interrupts if necessary */
482 /* data polling for D7 */
483 start = get_timer(0);
484 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
485 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {