3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Adapted for Interphase 4539 by Wolfgang Grandegger <wg@denx.de>.
10 * SPDX-License-Identifier: GPL-2.0+
18 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
20 extern int hwc_flash_size(void);
21 static ulong flash_get_size (u32 addr, flash_info_t *info);
22 static int flash_get_offsets (u32 base, flash_info_t *info);
23 static int write_word (flash_info_t *info, ulong dest, ulong data);
24 static void flash_reset (u32 addr);
26 #define out8(a,v) *(volatile unsigned char*)(a) = v
27 #define in8(a) *(volatile unsigned char*)(a)
28 #define in32(a) *(volatile unsigned long*)(a)
29 #define iobarrier_rw() eieio()
31 unsigned long flash_init (void)
34 unsigned long flash_size = 0;
35 unsigned long bank_size;
36 unsigned int bank = 0;
38 /* Init: no FLASHes known */
39 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
40 flash_info[i].flash_id = FLASH_UNKNOWN;
41 flash_info[i].sector_count = 0;
42 flash_info[i].size = 0;
45 /* Initialise the BOOT Flash */
46 if (bank == CONFIG_SYS_MAX_FLASH_BANKS) {
47 puts ("Warning: not all Flashes are initialised !");
51 bank_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info + bank);
53 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
54 CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MAX_FLASH_SIZE
55 /* monitor protection ON by default */
56 flash_protect(FLAG_PROTECT_SET,
57 CONFIG_SYS_MONITOR_BASE,
58 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
62 #ifdef CONFIG_ENV_IS_IN_FLASH
63 /* ENV protection ON by default */
64 flash_protect(FLAG_PROTECT_SET,
66 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
70 /* HWC protection ON by default */
71 flash_protect(FLAG_PROTECT_SET,
72 CONFIG_SYS_FLASH_BASE,
73 CONFIG_SYS_FLASH_BASE + 0x10000 - 1,
76 flash_size += bank_size;
79 puts ("Warning: the BOOT Flash is not initialised !");
86 * The following code cannot be run from FLASH!
88 static ulong flash_get_size (u32 addr, flash_info_t *info)
95 /* Write auto select command: read Manufacturer ID */
96 out8(addr + 0x0555, 0xAA);
99 out8(addr + 0x02AA, 0x55);
102 out8(addr + 0x0555, 0x90);
109 switch (value | (value << 16)) {
111 info->flash_id = FLASH_MAN_AMD;
115 info->flash_id = FLASH_MAN_FUJ;
119 info->flash_id = FLASH_UNKNOWN;
124 value = in8(addr + 1); /* device ID */
129 info->flash_id += FLASH_AM033C;
130 info->size = hwc_flash_size();
131 if (info->size > CONFIG_SYS_MAX_FLASH_SIZE) {
132 printf("U-Boot supports only %d MB\n",
133 CONFIG_SYS_MAX_FLASH_SIZE);
134 info->size = CONFIG_SYS_MAX_FLASH_SIZE;
136 info->sector_count = info->size / 0x10000;
140 info->flash_id = FLASH_UNKNOWN;
142 return (0); /* => no or unknown flash */
146 if (!flash_get_offsets (addr, info)) {
152 /* check for protected sectors */
153 for (i = 0; i < info->sector_count; i++) {
154 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
155 /* D0 = 1 if protected */
156 value = in8(info->start[i] + 2);
158 info->protect[i] = (value & 1) != 0;
163 * Reset bank to read mode
170 static int flash_get_offsets (u32 base, flash_info_t *info)
172 unsigned int i, size;
174 switch (info->flash_id & FLASH_TYPEMASK) {
176 /* set sector offsets for uniform sector type */
177 size = info->size / info->sector_count;
178 for (i = 0; i < info->sector_count; i++) {
179 info->start[i] = base + i * size;
189 int flash_erase (flash_info_t *info, int s_first, int s_last)
191 volatile u32 addr = info->start[0];
192 int flag, prot, sect, l_sect;
193 ulong start, now, last;
195 if (s_first < 0 || s_first > s_last) {
196 if (info->flash_id == FLASH_UNKNOWN) {
197 printf ("- missing\n");
199 printf ("- no sectors to erase\n");
204 if (info->flash_id == FLASH_UNKNOWN ||
205 info->flash_id > FLASH_AMD_COMP) {
206 printf ("Can't erase unknown flash type %08lx - aborted\n",
212 for (sect=s_first; sect<=s_last; ++sect) {
213 if (info->protect[sect]) {
219 printf ("- Warning: %d protected sectors will not be erased!\n",
227 /* Disable interrupts which might cause a timeout here */
228 flag = disable_interrupts();
230 out8(addr + 0x555, 0xAA);
232 out8(addr + 0x2AA, 0x55);
234 out8(addr + 0x555, 0x80);
236 out8(addr + 0x555, 0xAA);
238 out8(addr + 0x2AA, 0x55);
241 /* Start erase on unprotected sectors */
242 for (sect = s_first; sect<=s_last; sect++) {
243 if (info->protect[sect] == 0) { /* not protected */
244 addr = info->start[sect];
251 /* re-enable interrupts if necessary */
255 /* wait at least 80us - let's wait 1 ms */
259 * We wait for the last triggered sector
264 start = get_timer (0);
266 addr = info->start[l_sect];
267 while ((in8(addr) & 0x80) != 0x80) {
268 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
269 printf ("Timeout\n");
272 /* show that we're waiting */
273 if ((now - last) > 1000) { /* every second */
281 /* reset to read mode */
282 flash_reset (info->start[0]);
289 * Copy memory to flash, returns:
292 * 2 - Flash not erased
294 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
299 wp = (addr & ~3); /* get lower word aligned address */
302 * handle unaligned start bytes
304 if ((l = addr - wp) != 0) {
306 for (i=0, cp=wp; i<l; ++i, ++cp) {
307 data = (data << 8) | (*(uchar *)cp);
309 for (; i<4 && cnt>0; ++i) {
310 data = (data << 8) | *src++;
314 for (; cnt==0 && i<4; ++i, ++cp) {
315 data = (data << 8) | (*(uchar *)cp);
318 if ((rc = write_word(info, wp, data)) != 0) {
325 * handle word aligned part
329 for (i=0; i<4; ++i) {
330 data = (data << 8) | *src++;
332 if ((rc = write_word(info, wp, data)) != 0) {
344 * handle unaligned tail bytes
347 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
348 data = (data << 8) | *src++;
351 for (; i<4; ++i, ++cp) {
352 data = (data << 8) | (*(uchar *)cp);
355 return (write_word(info, wp, data));
359 * Write a word to Flash, returns:
362 * 2 - Flash not erased
364 static int write_word (flash_info_t *info, ulong dest, ulong data)
366 volatile u32 addr = info->start[0];
370 /* Check if Flash is (sufficiently) erased */
371 if ((in32(dest) & data) != data) {
374 /* Disable interrupts which might cause a timeout here */
375 flag = disable_interrupts();
377 /* first, perform an unlock bypass command to speed up flash writes */
378 out8(addr + 0x555, 0xAA);
380 out8(addr + 0x2AA, 0x55);
382 out8(addr + 0x555, 0x20);
385 /* write each byte out */
386 for (i = 0; i < 4; i++) {
387 char *data_ch = (char *)&data;
390 out8(dest+i, data_ch[i]);
392 udelay(10); /* XXX */
395 /* we're done, now do an unlock bypass reset */
401 /* re-enable interrupts if necessary */
405 /* data polling for D7 */
406 start = get_timer (0);
407 while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
408 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
420 * Reset bank to read mode
422 static void flash_reset (u32 addr)
424 out8(addr, 0xF0); /* reset bank */
428 void flash_print_info (flash_info_t *info)
432 if (info->flash_id == FLASH_UNKNOWN) {
433 printf ("missing or unknown FLASH type\n");
437 switch (info->flash_id & FLASH_VENDMASK) {
438 case FLASH_MAN_AMD: printf ("AMD "); break;
439 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
440 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
441 default: printf ("Unknown Vendor "); break;
444 switch (info->flash_id & FLASH_TYPEMASK) {
445 case FLASH_AM033C: printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
447 default: printf ("Unknown Chip Type\n");
451 if (info->size % 0x100000 == 0) {
452 printf (" Size: %ld MB in %d Sectors\n",
453 info->size / 0x100000, info->sector_count);
455 else if (info->size % 0x400 == 0) {
456 printf (" Size: %ld KB in %d Sectors\n",
457 info->size / 0x400, info->sector_count);
460 printf (" Size: %ld B in %d Sectors\n",
461 info->size, info->sector_count);
464 printf (" Sector Start Addresses:");
465 for (i=0; i<info->sector_count; ++i) {
470 info->protect[i] ? " (RO)" : " "