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 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 extern int hwc_flash_size(void);
37 static ulong flash_get_size (u32 addr, flash_info_t *info);
38 static int flash_get_offsets (u32 base, flash_info_t *info);
39 static int write_word (flash_info_t *info, ulong dest, ulong data);
40 static void flash_reset (u32 addr);
42 #define out8(a,v) *(volatile unsigned char*)(a) = v
43 #define in8(a) *(volatile unsigned char*)(a)
44 #define in32(a) *(volatile unsigned long*)(a)
45 #define iobarrier_rw() eieio()
47 unsigned long flash_init (void)
50 unsigned long flash_size = 0;
51 unsigned long bank_size;
52 unsigned int bank = 0;
54 /* Init: no FLASHes known */
55 for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
56 flash_info[i].flash_id = FLASH_UNKNOWN;
57 flash_info[i].sector_count = 0;
58 flash_info[i].size = 0;
61 /* Initialise the BOOT Flash */
62 if (bank == CFG_MAX_FLASH_BANKS) {
63 puts ("Warning: not all Flashes are initialised !");
67 bank_size = flash_get_size (CFG_FLASH_BASE, flash_info + bank);
69 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE && \
70 CFG_MONITOR_BASE < CFG_FLASH_BASE + CFG_MAX_FLASH_SIZE
71 /* monitor protection ON by default */
72 flash_protect(FLAG_PROTECT_SET,
74 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
78 #ifdef CFG_ENV_IS_IN_FLASH
79 /* ENV protection ON by default */
80 flash_protect(FLAG_PROTECT_SET,
82 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
86 /* HWC protection ON by default */
87 flash_protect(FLAG_PROTECT_SET,
89 CFG_FLASH_BASE + 0x10000 - 1,
92 flash_size += bank_size;
95 puts ("Warning: the BOOT Flash is not initialised !");
102 * The following code cannot be run from FLASH!
104 static ulong flash_get_size (u32 addr, flash_info_t *info)
106 volatile uchar value;
111 /* Write auto select command: read Manufacturer ID */
112 out8(addr + 0x0555, 0xAA);
115 out8(addr + 0x02AA, 0x55);
118 out8(addr + 0x0555, 0x90);
125 switch (value | (value << 16)) {
127 info->flash_id = FLASH_MAN_AMD;
131 info->flash_id = FLASH_MAN_FUJ;
135 info->flash_id = FLASH_UNKNOWN;
140 value = in8(addr + 1); /* device ID */
145 info->flash_id += FLASH_AM033C;
146 info->size = hwc_flash_size();
147 if (info->size > CFG_MAX_FLASH_SIZE) {
148 printf("U-Boot supports only %d MB\n",
150 info->size = CFG_MAX_FLASH_SIZE;
152 info->sector_count = info->size / 0x10000;
156 info->flash_id = FLASH_UNKNOWN;
158 return (0); /* => no or unknown flash */
162 if (!flash_get_offsets (addr, info)) {
168 /* check for protected sectors */
169 for (i = 0; i < info->sector_count; i++) {
170 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
171 /* D0 = 1 if protected */
172 value = in8(info->start[i] + 2);
174 info->protect[i] = (value & 1) != 0;
179 * Reset bank to read mode
186 static int flash_get_offsets (u32 base, flash_info_t *info)
188 unsigned int i, size;
190 switch (info->flash_id & FLASH_TYPEMASK) {
192 /* set sector offsets for uniform sector type */
193 size = info->size / info->sector_count;
194 for (i = 0; i < info->sector_count; i++) {
195 info->start[i] = base + i * size;
205 int flash_erase (flash_info_t *info, int s_first, int s_last)
207 volatile u32 addr = info->start[0];
208 int flag, prot, sect, l_sect;
209 ulong start, now, last;
211 if (s_first < 0 || s_first > s_last) {
212 if (info->flash_id == FLASH_UNKNOWN) {
213 printf ("- missing\n");
215 printf ("- no sectors to erase\n");
220 if (info->flash_id == FLASH_UNKNOWN ||
221 info->flash_id > FLASH_AMD_COMP) {
222 printf ("Can't erase unknown flash type %08lx - aborted\n",
228 for (sect=s_first; sect<=s_last; ++sect) {
229 if (info->protect[sect]) {
235 printf ("- Warning: %d protected sectors will not be erased!\n",
243 /* Disable interrupts which might cause a timeout here */
244 flag = disable_interrupts();
246 out8(addr + 0x555, 0xAA);
248 out8(addr + 0x2AA, 0x55);
250 out8(addr + 0x555, 0x80);
252 out8(addr + 0x555, 0xAA);
254 out8(addr + 0x2AA, 0x55);
257 /* Start erase on unprotected sectors */
258 for (sect = s_first; sect<=s_last; sect++) {
259 if (info->protect[sect] == 0) { /* not protected */
260 addr = info->start[sect];
267 /* re-enable interrupts if necessary */
271 /* wait at least 80us - let's wait 1 ms */
275 * We wait for the last triggered sector
280 start = get_timer (0);
282 addr = info->start[l_sect];
283 while ((in8(addr) & 0x80) != 0x80) {
284 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
285 printf ("Timeout\n");
288 /* show that we're waiting */
289 if ((now - last) > 1000) { /* every second */
297 /* reset to read mode */
298 flash_reset (info->start[0]);
305 * Copy memory to flash, returns:
308 * 2 - Flash not erased
310 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
315 wp = (addr & ~3); /* get lower word aligned address */
318 * handle unaligned start bytes
320 if ((l = addr - wp) != 0) {
322 for (i=0, cp=wp; i<l; ++i, ++cp) {
323 data = (data << 8) | (*(uchar *)cp);
325 for (; i<4 && cnt>0; ++i) {
326 data = (data << 8) | *src++;
330 for (; cnt==0 && i<4; ++i, ++cp) {
331 data = (data << 8) | (*(uchar *)cp);
334 if ((rc = write_word(info, wp, data)) != 0) {
341 * handle word aligned part
345 for (i=0; i<4; ++i) {
346 data = (data << 8) | *src++;
348 if ((rc = write_word(info, wp, data)) != 0) {
360 * handle unaligned tail bytes
363 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
364 data = (data << 8) | *src++;
367 for (; i<4; ++i, ++cp) {
368 data = (data << 8) | (*(uchar *)cp);
371 return (write_word(info, wp, data));
375 * Write a word to Flash, returns:
378 * 2 - Flash not erased
380 static int write_word (flash_info_t *info, ulong dest, ulong data)
382 volatile u32 addr = info->start[0];
386 /* Check if Flash is (sufficiently) erased */
387 if ((in32(dest) & data) != data) {
390 /* Disable interrupts which might cause a timeout here */
391 flag = disable_interrupts();
393 /* first, perform an unlock bypass command to speed up flash writes */
394 out8(addr + 0x555, 0xAA);
396 out8(addr + 0x2AA, 0x55);
398 out8(addr + 0x555, 0x20);
401 /* write each byte out */
402 for (i = 0; i < 4; i++) {
403 char *data_ch = (char *)&data;
406 out8(dest+i, data_ch[i]);
408 udelay(10); /* XXX */
411 /* we're done, now do an unlock bypass reset */
417 /* re-enable interrupts if necessary */
421 /* data polling for D7 */
422 start = get_timer (0);
423 while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
424 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
436 * Reset bank to read mode
438 static void flash_reset (u32 addr)
440 out8(addr, 0xF0); /* reset bank */
444 void flash_print_info (flash_info_t *info)
448 if (info->flash_id == FLASH_UNKNOWN) {
449 printf ("missing or unknown FLASH type\n");
453 switch (info->flash_id & FLASH_VENDMASK) {
454 case FLASH_MAN_AMD: printf ("AMD "); break;
455 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
456 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
457 default: printf ("Unknown Vendor "); break;
460 switch (info->flash_id & FLASH_TYPEMASK) {
461 case FLASH_AM033C: printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
463 default: printf ("Unknown Chip Type\n");
467 if (info->size % 0x100000 == 0) {
468 printf (" Size: %ld MB in %d Sectors\n",
469 info->size / 0x100000, info->sector_count);
471 else if (info->size % 0x400 == 0) {
472 printf (" Size: %ld KB in %d Sectors\n",
473 info->size / 0x400, info->sector_count);
476 printf (" Size: %ld B in %d Sectors\n",
477 info->size, info->sector_count);
480 printf (" Sector Start Addresses:");
481 for (i=0; i<info->sector_count; ++i) {
486 info->protect[i] ? " (RO)" : " "