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];
16 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
17 static int write_word(flash_info_t *info, ulong dest, ulong data);
18 static void flash_get_offsets(ulong base, flash_info_t *info);
20 unsigned long flash_init(void)
22 unsigned long size_b0;
25 /* Init: no FLASHes known */
26 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
27 flash_info[i].flash_id = FLASH_UNKNOWN;
30 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
34 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
36 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
37 /* Monitor protection ON by default */
38 flash_protect(FLAG_PROTECT_SET,
39 CONFIG_SYS_MONITOR_BASE,
40 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
44 flash_info[0].size = size_b0;
49 /*-----------------------------------------------------------------------
50 * Fix this to support variable sector sizes
52 static void flash_get_offsets(ulong base, flash_info_t *info)
56 /* set up sector start address table */
57 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
58 /* set sector offsets for bottom boot block type */
59 for (i = 0; i < info->sector_count; i++)
60 info->start[i] = base + (i * 0x00010000);
64 /*-----------------------------------------------------------------------
66 void flash_print_info(flash_info_t *info)
70 if (info->flash_id == FLASH_UNKNOWN) {
71 puts("missing or unknown FLASH type\n");
75 switch (info->flash_id & FLASH_VENDMASK) {
83 printf("BRIGHT MICRO ");
86 printf("Unknown Vendor ");
90 switch (info->flash_id & FLASH_TYPEMASK) {
92 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
95 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
98 printf("AM29LV400T (4 Mbit, top boot sector)\n");
101 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
104 printf("AM29LV800T (8 Mbit, top boot sector)\n");
107 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
110 printf("AM29LV160T (16 Mbit, top boot sector)\n");
113 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
116 printf("AM29LV320T (32 Mbit, top boot sector)\n");
119 printf("Unknown Chip Type\n");
123 if (info->size >> 20) {
124 printf(" Size: %ld MB in %d Sectors\n",
128 printf(" Size: %ld KB in %d Sectors\n",
133 puts(" Sector Start Addresses:");
135 for (i = 0; i < info->sector_count; ++i) {
141 info->protect[i] ? " (RO)" : " ");
149 * The following code cannot be run from FLASH!
152 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
155 volatile unsigned char *caddr;
158 caddr = (volatile unsigned char *)addr ;
160 /* Write auto select command: read Manufacturer ID */
162 debug("Base address is: %8p\n", caddr);
164 caddr[0x0555] = 0xAA;
165 caddr[0x02AA] = 0x55;
166 caddr[0x0555] = 0x90;
170 debug("Manufact ID: %02x\n", value);
173 case 0x01: /*AMD_MANUFACT*/
174 info->flash_id = FLASH_MAN_AMD;
177 case 0x04: /*FUJ_MANUFACT*/
178 info->flash_id = FLASH_MAN_FUJ;
182 info->flash_id = FLASH_UNKNOWN;
183 info->sector_count = 0;
188 value = caddr[1]; /* device ID */
190 debug("Device ID: %02x\n", value);
194 info->flash_id += FLASH_AM040;
195 info->sector_count = 8;
196 info->size = 0x00080000;
197 break; /* => 512Kb */
200 info->flash_id = FLASH_UNKNOWN;
201 return 0; /* => no or unknown flash */
204 flash_get_offsets((ulong)addr, &flash_info[0]);
206 /* check for protected sectors */
207 for (i = 0; i < info->sector_count; i++) {
209 * read sector protection at sector address,
211 * D0 = 1 if protected
213 caddr = (volatile unsigned char *)(info->start[i]);
214 info->protect[i] = caddr[2] & 1;
218 * Prevent writes to uninitialized FLASH.
220 if (info->flash_id != FLASH_UNKNOWN) {
221 caddr = (volatile unsigned char *)info->start[0];
222 *caddr = 0xF0; /* reset bank */
229 int flash_erase(flash_info_t *info, int s_first, int s_last)
231 volatile unsigned char *addr =
232 (volatile unsigned char *)(info->start[0]);
233 int flag, prot, sect, l_sect;
234 ulong start, now, last;
236 if ((s_first < 0) || (s_first > s_last)) {
237 if (info->flash_id == FLASH_UNKNOWN)
238 printf("- missing\n");
240 printf("- no sectors to erase\n");
245 if ((info->flash_id == FLASH_UNKNOWN) ||
246 (info->flash_id > FLASH_AMD_COMP)) {
247 printf("Can't erase unknown flash type - aborted\n");
252 for (sect = s_first; sect <= s_last; ++sect) {
253 if (info->protect[sect])
258 printf("- Warning: %d protected sectors will not be erased!\n",
266 /* Disable interrupts which might cause a timeout here */
267 flag = disable_interrupts();
275 /* Start erase on unprotected sectors */
276 for (sect = s_first; sect <= s_last; sect++) {
277 if (info->protect[sect] == 0) { /* not protected */
278 addr = (volatile unsigned char *)(info->start[sect]);
284 /* re-enable interrupts if necessary */
288 /* wait at least 80us - let's wait 1 ms */
292 * We wait for the last triggered sector
297 start = get_timer(0);
299 addr = (volatile unsigned char *)(info->start[l_sect]);
301 while ((addr[0] & 0xFF) != 0xFF) {
302 now = get_timer(start);
303 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
307 /* show that we're waiting */
308 if ((now - last) > 1000) { /* every second */
315 /* reset to read mode */
316 addr = (volatile unsigned char *)info->start[0];
318 addr[0] = 0xF0; /* reset bank */
325 * Copy memory to flash, returns:
328 * 2 - Flash not erased
331 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
336 wp = (addr & ~3); /* get lower word aligned address */
339 * handle unaligned start bytes
345 for (i = 0, cp = wp; i < l; ++i, ++cp)
346 data = (data << 8) | (*(uchar *)cp);
348 for (; i < 4 && cnt > 0; ++i) {
349 data = (data << 8) | *src++;
354 for (; cnt == 0 && i < 4; ++i, ++cp)
355 data = (data << 8) | (*(uchar *)cp);
357 rc = write_word(info, wp, data);
366 * handle word aligned part
370 for (i = 0; i < 4; ++i)
371 data = (data << 8) | *src++;
373 rc = write_word(info, wp, data);
386 * handle unaligned tail bytes
389 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
390 data = (data << 8) | *src++;
393 for (; i < 4; ++i, ++cp)
394 data = (data << 8) | (*(uchar *)cp);
396 return write_word(info, wp, data);
400 * Write a word to Flash, returns:
403 * 2 - Flash not erased
405 static int write_word(flash_info_t *info, ulong dest, ulong data)
407 volatile unsigned char *cdest, *cdata;
408 volatile unsigned char *addr =
409 (volatile unsigned char *)(info->start[0]);
411 int flag, count = 4 ;
413 cdest = (volatile unsigned char *)dest ;
414 cdata = (volatile unsigned char *)&data ;
416 /* Check if Flash is (sufficiently) erased */
417 if ((*((vu_long *)dest)&data) != data)
421 /* Disable interrupts which might cause a timeout here */
422 flag = disable_interrupts();
430 /* re-enable interrupts if necessary */
434 /* data polling for D7 */
435 start = get_timer(0);
436 while ((*cdest ^ *cdata) & 0x80) {
437 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)