欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

flash.c

适合KS8695X
C
第 1 页 / 共 2 页
字号:
/*
 * (C) Copyright 2005
 * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
 *
 * (C) Copyright 2001
 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
 *
 * (C) Copyright 2001
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <linux/byteorder/swab.h>

static void out8(ulong addr, uchar value)
{
         *(uchar *)addr=value;
}

static uchar in8(ulong addr)
{
        return *(uchar *)addr;
}

#define iobarrier_rw()  {}

static void flash_reset (ulong addr)
{
	    /* reset bank */
        //out8(addr, 0xF0);       
         *(volatile unsigned short *)addr = 0xF0;
        iobarrier_rw();
}

flash_info_t flash_info[CFG_MAX_FLASH_BANKS];	/* info for FLASH chips */

#define mb() __asm__ __volatile__ ("" : : : "memory")

/*-----------------------------------------------------------------------
 * Functions
 */
static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
//static int write_data (flash_info_t * info, ulong dest, unsigned short data);
static void flash_get_offsets (ulong base, flash_info_t * info);
void inline spin_wheel (void);

/*-----------------------------------------------------------------------
 */

unsigned long flash_init (void)
{
	int i;
	ulong size = 0;

	for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
		switch (i) {
		case 0:
			flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
			flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
			break;
		case 1:
			/* ignore for now */
			flash_info[i].flash_id = FLASH_UNKNOWN;
			break;
		default:
			panic ("configured too many flash banks!\n");
			break;
		}
		size += flash_info[i].size;
	}

	/* Protect monitor and environment sectors
	 */

	flash_protect (FLAG_PROTECT_SET,
		       CFG_FLASH_BASE,
		       CFG_FLASH_BASE + (U_BOOT_CODE_SIZE + CFG_ENV_SECT_SIZE - 1),
		       &flash_info[0]);

	return size;
}

/*-----------------------------------------------------------------------
 */
static void flash_get_offsets (ulong base, flash_info_t * info)
{
	int i;

	if (info->flash_id == FLASH_UNKNOWN)
		return;

	/* set up sector start address table */
	if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
		for (i = 0; i < info->sector_count; i++) {
			info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
			info->protect[i] = 0;
		}
	}

	else if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
		(info->flash_id  == FLASH_AM040) ||
		(info->flash_id == FLASH_AMDLV033C) ||
		(info->flash_id == FLASH_AMDLV065D)) {
		ulong sectsize = info->size / info->sector_count;
		for (i = 0; i < info->sector_count; i++)
			info->start[i] = base + (i * sectsize);
	} else {
	    if (info->flash_id & FLASH_BTYPE) {
		/* set sector offsets for bottom boot block type	*/
		info->start[0] = base + 0x00000000;
		info->start[1] = base + 0x00004000;
		info->start[2] = base + 0x00006000;
		info->start[3] = base + 0x00008000;
		for (i = 4; i < info->sector_count; i++) {
			info->start[i] = base + (i * 0x00010000) - 0x00030000;
		}
	    } else {
		/* set sector offsets for top boot block type		*/
		i = info->sector_count - 1;
		info->start[i--] = base + info->size - 0x00004000;
		info->start[i--] = base + info->size - 0x00006000;
		info->start[i--] = base + info->size - 0x00008000;
		for (; i >= 0; i--) {
			info->start[i] = base + i * 0x00010000;
		}
	    }
	}

}

/*-----------------------------------------------------------------------
 */
void flash_print_info (flash_info_t * info)
{
	int i;

	if (info->flash_id == FLASH_UNKNOWN) {
		printf ("missing or unknown FLASH type\n");
		return;
	}

	switch (info->flash_id & FLASH_VENDMASK) {
	case FLASH_MAN_AMD:	printf ("AMD ");		break;
	case FLASH_MAN_INTEL:	printf ("INTEL ");		break;
	case FLASH_MAN_FUJ:	printf ("FUJITSU ");		break;
	case FLASH_MAN_SST:	printf ("SST ");		break;
	case FLASH_MAN_MX:	printf ("MXIC ");		break;

	default:		printf ("Unknown Vendor ");	break;
	}

	switch (info->flash_id & FLASH_TYPEMASK) {
	case FLASH_AM040:	printf ("AM29F040 (512 Kbit, uniform sector size)\n");
				break;
	case FLASH_AM400B:	printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
				break;
	case FLASH_AM400T:	printf ("AM29LV400T (4 Mbit, top boot sector)\n");
				break;
	case FLASH_AM800B:	printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
				break;
	case FLASH_AM800T:	printf ("AM29LV800T (8 Mbit, top boot sector)\n");
				break;
	case FLASH_AM160B:	printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
				break;
	case FLASH_AM160T:	printf ("AM29LV160T (16 Mbit, top boot sector)\n");
				break;
	case FLASH_AM320B:	printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
				break;
	case FLASH_AM320T:	printf ("AM29LV320T (32 Mbit, top boot sector)\n");
				break;	
	case FLASH_MXLV320B:	printf ("FLASH_MXLV320B (32 Mbit, top boot sector)\n");
				break;
	case FLASH_MXLV320T:	printf ("FLASH_MXLV320T (32 Mbit, top boot sector)\n");
				break;
	case FLASH_AMDLV033C:	printf ("AM29LV033C (32 Mbit, uniform sector size)\n");
				break;
	case FLASH_AMDLV065D:	printf ("AM29LV065D (64 Mbit, uniform sector size)\n");
				break;
	case FLASH_SST800A:	printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
				break;
	case FLASH_SST160A:	printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
				break;
	case FLASH_SST040:	printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
				break;
	default:		printf ("Unknown Chip Type\n");
				break;
	}

	printf ("  Size: %ld KB in %d Sectors\n",
		info->size >> 10, info->sector_count);

	printf ("  Sector Start Addresses:");
	for (i=0; i<info->sector_count; ++i) {
		if ((i % 5) == 0)
			printf ("\n   ");
		printf (" %08lX%s",
			info->start[i],
			info->protect[i] ? " (RO)" : "     "
		);
	}

	printf ("\n");
	return;
}

/*
 * The following code cannot be run from FLASH!
 */
static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
{
	//volatile unsigned char value1,value2,value3;

/*	
	volatile unsigned char value;
    out8(addr + 0x0555, 0xAA);
    //iobarrier_rw();
    out8(addr + 0x02AA, 0x55);
    //iobarrier_rw();
    out8(addr + 0x0555, 0x90);
    //iobarrier_rw();
    value = in8(addr);
    iobarrier_rw();
*/	
	
	volatile unsigned short value;
	volatile unsigned short *flashPtr;
		
	*((volatile unsigned short *)addr + 0x555) = 0xaa; 
	*((volatile unsigned short *)addr + 0x2aa) = 0x55;
    *((volatile unsigned short *)addr + 0x555) = 0x90;
    
	*flashPtr = *((volatile unsigned short *)addr); 
	
	value = *flashPtr;

	debug ("Flash start address =0x%x\n", addr);
	    
	debug ("Flash ID =0x%x\n", value);

        switch (value) {

        case (INTEL_MANUFACT & 0xFFFF):
                debug ("Manufacturer: Intel (not supported yet)\n");
                info->flash_id = FLASH_MAN_INTEL;
                break;

        case (AMD_MANUFACT & 0xFFFF):
                debug ("Manufacturer: AMD (Spansion)\n");
                info->flash_id = FLASH_MAN_AMD;
                break;

        case (SST_MANUFACT & 0xFFFF):
                debug ("Manufacturer: SST\n");
                info->flash_id = FLASH_MAN_SST;
                break;

        case (FUJ_MANUFACT & 0xFFFF):
                debug ("Manufacturer: FUJITSU (Spansion)\n");
                info->flash_id = FLASH_MAN_FUJ;
                break;

        case (MX_MANUFACT & 0xFFFF):
                debug ("Manufacturer: MX\n");
                info->flash_id = FLASH_MAN_MX;
                break;
		default:
                debug ("Undefined Manufacturer ID\n");
				info->flash_id = FLASH_UNKNOWN;
				info->sector_count = 0;
				info->size = 0;
				addr[0] = 0xFF;	/* restore read mode */
				return (0);	/* no or unknown flash  */
       break;
	}

#ifdef DEL
	mb ();
	value = addr[1];	/* device ID            */
#endif
/*
    out8(addr + 0x0555, 0xAA);
    iobarrier_rw();
    out8(addr + 0x02AA, 0x55);
    iobarrier_rw();
    out8(addr + 0x0555, 0x90);
    iobarrier_rw();
    value = in8(addr+1);
    iobarrier_rw();
*/

	*((volatile unsigned short *)addr + 0x555) = 0xaa; 
	*((volatile unsigned short *)addr + 0x2aa) = 0x55;
    *((volatile unsigned short *)addr + 0x555) = 0x90;
    
	*flashPtr = *((volatile unsigned short *)addr+1); 
	
	value = *flashPtr;
	
	debug (" Device: ID= 0x%x\n", value);

	switch ((volatile unsigned char)value) {

	case (unsigned char)INTEL_ID_28F640J3A:
		info->flash_id += FLASH_28F640J3A;
		info->sector_count = 64;
		info->size = 0x00800000;
		break;		/* => 8 MB     */

	case (unsigned char)INTEL_ID_28F128J3A:
		info->flash_id += FLASH_28F128J3A;
		info->sector_count = 128;
		info->size = 0x01000000;
		break;		/* => 16 MB     */

	case (unsigned char)AMD_ID_F040B:
		info->flash_id += FLASH_AM040;
		info->sector_count = 8;
		info->size = 0x0080000; /* => 512 ko */
		break;

	case (unsigned char)AMD_ID_LV400T:
		info->flash_id += FLASH_AM400T;
		info->sector_count = 11;
		info->size = 0x00080000;
		break;				/* => 0.5 MB		*/

	case (unsigned char)AMD_ID_LV400B:
		info->flash_id += FLASH_AM400B;
		info->sector_count = 11;
		info->size = 0x00080000;
		break;				/* => 0.5 MB		*/

	case (unsigned char)AMD_ID_LV800T:
		info->flash_id += FLASH_AM800T;
		info->sector_count = 19;
		info->size = 0x00100000;
		break;				/* => 1 MB		*/

	case (unsigned char)AMD_ID_LV800B:
		info->flash_id += FLASH_AM800B;
		info->sector_count = 19;
		info->size = 0x00100000;
		break;				/* => 1 MB		*/

	case (unsigned char)AMD_ID_LV160T:
		info->flash_id += FLASH_AM160T;
		info->sector_count = 35;
		info->size = 0x00200000;
		break;				/* => 2 MB		*/

	case (unsigned char)AMD_ID_LV160B:
		info->flash_id += FLASH_AM160B;
		info->sector_count = 35;
		info->size = 0x00200000;
		break;				/* => 2 MB		*/

	case (unsigned char)AMD_ID_LV033C:
		info->flash_id += FLASH_AMDLV033C;
		info->sector_count = 64;
		info->size = 0x00400000;
		break;				/* => 4 MB		*/

	case (unsigned char)AMD_ID_LV065D:
		info->flash_id += FLASH_AMDLV065D;
		info->sector_count = 128;
		info->size = 0x00800000;
		break;				/* => 8 MB		*/

	case (unsigned char)AMD_ID_LV320T:
		info->flash_id += FLASH_AM320T;
		info->sector_count = 67;
		info->size = 0x00400000;
		break;				/* => 4 MB		*/

	case (unsigned char)AMD_ID_LV320B:
		info->flash_id += FLASH_AM320B;
		info->sector_count = 67;
		info->size = 0x00400000;
		break;				/* => 4 MB		*/

	case (unsigned char)MX_ID_LV320B:
		info->flash_id += FLASH_MXLV320B;
		info->sector_count = 67;
		info->size = 0x00400000;
		break;				/* => 4 MB		*/
	case (unsigned char)MX_ID_LV320T:
		info->flash_id += FLASH_MXLV320T;
		info->sector_count = 67;
		info->size = 0x00400000;
		break;				/* => 4 MB		*/

	case (unsigned char)SST_ID_xF800A:
		info->flash_id += FLASH_SST800A;
		info->sector_count = 16;
		info->size = 0x00100000;
		break;				/* => 1 MB		*/

	case (unsigned char)SST_ID_xF160A:
		info->flash_id += FLASH_SST160A;
		info->sector_count = 32;
		info->size = 0x00200000;
		break;				/* => 2 MB		*/
	case (unsigned char)SST_ID_xF040:
		info->flash_id += FLASH_SST040;
		info->sector_count = 128;
		info->size = 0x00080000;
		break;				/* => 512KB		*/

	default:
        debug ("Undefined Device ID\n");
		info->flash_id = FLASH_UNKNOWN;
		break;
	}

	if (info->sector_count > CFG_MAX_FLASH_SECT) {
		printf ("** ERROR: sector count %d > max (%d) **\n",
			info->sector_count, CFG_MAX_FLASH_SECT);
		info->sector_count = CFG_MAX_FLASH_SECT;
	}
#ifdef UBOOT_ORIG
	addr[0] = 0xFF;	/* restore read mode */
#endif

	flash_reset(addr);

	return (info->size);
}


/*-----------------------------------------------------------------------
 */

int flash_erase (flash_info_t * info, int s_first, int s_last)
{
	volatile u32 addr = info->start[0];
	int flag, prot, sect, l_sect;
	ulong type;
	int rcode = 0;
	ulong start, now, last;

	if ((s_first < 0) || (s_first > s_last)) {
		if (info->flash_id == FLASH_UNKNOWN) {
			printf ("- missing\n");
		} else {
			printf ("- no sectors to erase\n");
		}
		return 1;
	}

	type = (info->flash_id & FLASH_VENDMASK);
	if (info->flash_id == FLASH_UNKNOWN) {
		printf ("Can't erase unknown flash type %08lx - aborted\n",
			info->flash_id);
		return 1;
	}

	prot = 0;
	for (sect = s_first; sect <= s_last; ++sect) {
		if (info->protect[sect]) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -