2 * (C) Copyright 2003, Dan Malek, Embedded Edge, LLC. <dan@embeddededge.com>
4 * Updated to support the Silicon Tx GP3 8560. We should only find
5 * two Intel 28F640 parts in 16-bit mode (i.e. 32-bit wide flash),
6 * but I left other code here in case people order custom boards.
8 * (C) Copyright 2003 Motorola Inc.
9 * Xianghua Xiao,(X.Xiao@motorola.com)
11 * (C) Copyright 2000, 2001
12 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
15 * Add support the Sharp chips on the mpc8260ads.
16 * I started with board/ip860/flash.c and made changes I found in
17 * the MTD project by David Schleef.
19 * SPDX-License-Identifier: GPL-2.0+
24 #if !defined(CONFIG_SYS_NO_FLASH)
26 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
28 #if defined(CONFIG_ENV_IS_IN_FLASH)
29 # ifndef CONFIG_ENV_ADDR
30 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
32 # ifndef CONFIG_ENV_SIZE
33 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
35 # ifndef CONFIG_ENV_SECT_SIZE
36 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
42 /*-----------------------------------------------------------------------
45 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
46 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 static int clear_block_lock_bit(vu_long * addr);
48 /*-----------------------------------------------------------------------
51 unsigned long flash_init (void)
56 /* Init: enable write,
57 * or we cannot even write flash commands
59 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
60 flash_info[i].flash_id = FLASH_UNKNOWN;
62 /* set the default sector offset */
65 /* Static FLASH Bank configuration here - FIXME XXX */
67 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
69 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
70 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
74 /* Re-do sizing to get full correct info */
75 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
77 flash_info[0].size = size;
79 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
80 /* monitor protection ON by default */
81 flash_protect(FLAG_PROTECT_SET,
82 CONFIG_SYS_MONITOR_BASE,
83 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
86 #ifdef CONFIG_ENV_IS_IN_FLASH
87 /* ENV protection ON by default */
88 flash_protect(FLAG_PROTECT_SET,
90 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
97 /*-----------------------------------------------------------------------
99 void flash_print_info (flash_info_t *info)
103 if (info->flash_id == FLASH_UNKNOWN) {
104 printf ("missing or unknown FLASH type\n");
108 switch (info->flash_id & FLASH_VENDMASK) {
109 case FLASH_MAN_INTEL: printf ("Intel "); break;
110 case FLASH_MAN_SHARP: printf ("Sharp "); break;
111 default: printf ("Unknown Vendor "); break;
114 switch (info->flash_id & FLASH_TYPEMASK) {
115 case FLASH_28F640C3T: printf ("28F640C3T (64 Mbit x 2, 128 x 128k)\n");
117 default: printf ("Unknown Chip Type\n");
121 printf (" Size: %ld MB in %d Sectors\n",
122 info->size >> 20, info->sector_count);
124 printf (" Sector Start Addresses:");
125 for (i=0; i<info->sector_count; ++i) {
130 info->protect[i] ? " (RO)" : " "
137 * The following code cannot be run from FLASH!
140 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
144 ulong base = (ulong)addr;
148 printf("Check flash at 0x%08x\n",(uint)addr);
150 /* Write "Intelligent Identifier" command: read Manufacturer ID */
155 value = addr[0] & 0x00FF00FF;
158 printf("manufacturer=0x%x\n",(uint)value);
161 case MT_MANUFACT: /* SHARP, MT or => Intel */
163 info->flash_id = FLASH_MAN_INTEL;
166 printf("unknown manufacturer: %x\n", (unsigned int)value);
167 info->flash_id = FLASH_UNKNOWN;
168 info->sector_count = 0;
170 return (0); /* no or unknown flash */
173 value = addr[1]; /* device ID */
176 printf("deviceID=0x%x\n",(uint)value);
180 case (INTEL_ID_28F640C3T):
181 info->flash_id += FLASH_28F640C3T;
182 info->sector_count = 135;
183 info->size = 0x01000000;
184 sector_offset = 0x20000;
185 break; /* => 2x8 MB */
188 info->flash_id = FLASH_UNKNOWN;
189 return (0); /* => no or unknown flash */
193 /* set up sector start address table
194 * The first 127 blocks are large, the last 8 are small.
196 for (i = 0; i < 127; i++) {
197 info->start[i] = base;
198 base += sector_offset;
199 /* Sectors are locked upon reset */
200 info->protect[i] = 0;
202 for (i = 127; i < 135; i++) {
203 info->start[i] = base;
205 /* Sectors are locked upon reset */
206 info->protect[i] = 0;
211 * Prevent writes to uninitialized FLASH.
213 if (info->flash_id != FLASH_UNKNOWN) {
214 addr = (vu_long *)info->start[0];
215 *addr = 0xFFFFFF; /* reset bank to read array mode */
223 /*-----------------------------------------------------------------------
226 int flash_erase (flash_info_t *info, int s_first, int s_last)
228 int flag, prot, sect;
229 ulong start, now, last;
231 if ((s_first < 0) || (s_first > s_last)) {
232 if (info->flash_id == FLASH_UNKNOWN) {
233 printf ("- missing\n");
235 printf ("- no sectors to erase\n");
240 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
241 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
242 printf ("Can't erase unknown flash type %08lx - aborted\n",
248 for (sect=s_first; sect<=s_last; ++sect) {
249 if (info->protect[sect]) {
255 printf ("- Warning: %d protected sectors will not be erased!\n",
262 printf("\nFlash Erase:\n");
264 /* Make Sure Block Lock Bit is not set. */
265 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
269 /* Start erase on unprotected sectors */
271 printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
273 for (sect = s_first; sect<=s_last; sect++) {
274 if (info->protect[sect] == 0) { /* not protected */
275 vu_long *addr = (vu_long *)(info->start[sect]);
278 last = start = get_timer (0);
280 /* Disable interrupts which might cause a timeout here */
281 flag = disable_interrupts();
286 /* Clear Status Register */
289 /* Single Block Erase Command */
296 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
297 /* Resume Command, as per errata update */
302 /* re-enable interrupts if necessary */
306 /* wait at least 80us - let's wait 1 ms */
308 while ((*addr & 0x00800080) != 0x00800080) {
309 if(*addr & 0x00200020){
310 printf("Error in Block Erase - Lock Bit may be set!\n");
311 printf("Status Register = 0x%X\n", (uint)*addr);
312 *addr = 0xFFFFFFFF; /* reset bank */
316 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
317 printf ("Timeout\n");
318 *addr = 0xFFFFFFFF; /* reset bank */
322 /* show that we're waiting */
323 if ((now - last) > 1000) { /* every second */
329 /* reset to read mode */
335 printf ("flash erase done\n");
339 /*-----------------------------------------------------------------------
340 * Copy memory to flash, returns:
343 * 2 - Flash not erased
346 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
351 wp = (addr & ~3); /* get lower word aligned address */
354 * handle unaligned start bytes
356 if ((l = addr - wp) != 0) {
358 for (i=0, cp=wp; i<l; ++i, ++cp) {
359 data = (data << 8) | (*(uchar *)cp);
361 for (; i<4 && cnt>0; ++i) {
362 data = (data << 8) | *src++;
366 for (; cnt==0 && i<4; ++i, ++cp) {
367 data = (data << 8) | (*(uchar *)cp);
370 if ((rc = write_word(info, wp, data)) != 0) {
377 * handle word aligned part
381 for (i=0; i<4; ++i) {
382 data = (data << 8) | *src++;
384 if ((rc = write_word(info, wp, data)) != 0) {
396 * handle unaligned tail bytes
399 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
400 data = (data << 8) | *src++;
403 for (; i<4; ++i, ++cp) {
404 data = (data << 8) | (*(uchar *)cp);
407 return (write_word(info, wp, data));
410 /*-----------------------------------------------------------------------
411 * Write a word to Flash, returns:
414 * 2 - Flash not erased
416 static int write_word (flash_info_t *info, ulong dest, ulong data)
418 vu_long *addr = (vu_long *)dest;
422 /* Check if Flash is (sufficiently) erased */
423 if ((*addr & data) != data) {
426 /* Disable interrupts which might cause a timeout here */
427 flag = disable_interrupts();
436 /* re-enable interrupts if necessary */
440 /* data polling for D7 */
441 start = get_timer (0);
444 while (((csr = *addr) & 0x00800080) != 0x00800080) {
445 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
450 if (csr & 0x40404040) {
451 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
455 /* Clear Status Registers Command */
458 /* Reset to read array mode */
465 /*-----------------------------------------------------------------------
466 * Clear Block Lock Bit, returns:
471 static int clear_block_lock_bit(vu_long * addr)
478 /* Clear Status Register */
487 start = get_timer (0);
488 while((*addr & 0x00800080) != 0x00800080){
489 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
490 printf ("Timeout on clearing Block Lock Bit\n");
491 *addr = 0xFFFFFFFF; /* reset bank */
499 #endif /* !CONFIG_SYS_NO_FLASH */