⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.c

📁 U-Boot1.1.2是最为常用的嵌入式系统Bootloader
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * MOUSSE/MPC8240 Board definitions. * Flash Routines for MOUSSE onboard AMD29LV106DB devices * * (C) Copyright 2000 * Marius Groeger <mgroeger@sysgo.de> * Sysgo Real-Time Solutions, GmbH <www.elinos.com> * * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * (C) Copyright 1999, by Curt McDowell, 08-06-99, Broadcom Corp. * (C) Copyright 2001, James Dougherty, 07/18/01, Broadcom Corp. * * 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 <mpc8xx.h>#include <malloc.h>#include "mousse.h"#include "flash.h"int flashLibDebug = 0;int flashLibInited = 0;#define OK  0#define ERROR -1#define STATUS int#define PRINTF			if (flashLibDebug) printf#if 0#define PRIVATE			static#else#define PRIVATE#endifflash_info_t flash_info[CFG_MAX_FLASH_BANKS];#define SLEEP_DELAY    166#define FLASH_SECTOR_SIZE   (64*1024)/*********************************************************************** * * Virtual Flash Devices on Mousse board * * These must be kept in sync with the definitions in flashLib.h. * ***********************************************************************/PRIVATE flash_dev_t flashDev[] = {    /* Bank 0 sector SA0 (16 kB) */    {	"SA0",FLASH0_BANK, FLASH0_SEG0_START, 1, 14,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },    /* Bank 0 sector SA1 (8 kB) */    {	"SA1", FLASH0_BANK, FLASH0_SEG0_START + 0x4000, 1, 13,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },    /* Bank 0 sector SA2 (8 kB) */    {	"SA2", FLASH0_BANK, FLASH0_SEG0_START + 0x6000, 1, 13,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },    /* Bank 0 sector SA3 is occluded by Mousse I/O devices */    /* Bank 0 sectors SA4-SA18, after Mousse devices up to PLCC (960 kB)  */    {	"KERNEL", FLASH0_BANK, FLASH0_SEG1_START, 15, 16,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },    /* Bank 0 sectors SA19-SA26, jumper can occlude this by PLCC (512 kB) */    /* This is where the Kahlua boot vector and boot ROM code resides. */    {	"BOOT",FLASH0_BANK, FLASH0_SEG2_START, 8, 16,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },    /* Bank 0 sectors SA27-SA34 (512 kB) */    {	"RAMDISK",FLASH0_BANK, FLASH0_SEG3_START, 8, 16,	FLASH0_VENDOR_ID, FLASH0_DEVICE_ID    },};int flashDevCount = (sizeof (flashDev) / sizeof (flashDev[0]));#define DEV(no)			(&flashDev[no])#define DEV_NO(dev)		((dev) - flashDev)/*********************************************************************** * * Private Flash Routines * ***********************************************************************//* * The convention is: * * "addr" is always the PROM raw address, which is the address of an * 8-bit quantity for flash 0 and 16-bit quantity for flash 1. * * "pos" is always a logical byte position from the PROM beginning. */#define FLASH0_ADDR(dev, addr) \	((unsigned char *) ((dev)->base + (addr)))#define FLASH0_WRITE(dev, addr, value) \	(*FLASH0_ADDR(dev, addr) = (value))#define FLASH0_READ(dev, addr) \	(*FLASH0_ADDR(dev, addr))PRIVATE int flashCheck (flash_dev_t * dev){	if (!flashLibInited) {		printf ("flashCheck: flashLib not initialized\n");		return ERROR;	}	if (dev < &flashDev[0] || dev >= &flashDev[flashDevCount]) {		printf ("flashCheck: Bad dev parameter\n");		return ERROR;	}	if (!dev->found) {		printf ("flashCheck: Device %d not available\n", DEV_NO (dev));		return ERROR;	}	return OK;}PRIVATE void flashReset (flash_dev_t * dev){	PRINTF ("flashReset: dev=%d\n", DEV_NO (dev));	if (dev->bank == FLASH0_BANK) {		FLASH0_WRITE (dev, 0x555, 0xaa);		FLASH0_WRITE (dev, 0xaaa, 0x55);		FLASH0_WRITE (dev, 0x555, 0xf0);	}	udelay (SLEEP_DELAY);	PRINTF ("flashReset: done\n");}PRIVATE int flashProbe (flash_dev_t * dev){	int rv, deviceID, vendorID;	PRINTF ("flashProbe: dev=%d\n", DEV_NO (dev));	if (dev->bank != FLASH0_BANK) {		rv = ERROR;		goto DONE;	}	FLASH0_WRITE (dev, 0xaaa, 0xaa);	FLASH0_WRITE (dev, 0x555, 0x55);	FLASH0_WRITE (dev, 0xaaa, 0x90);	udelay (SLEEP_DELAY);	vendorID = FLASH0_READ (dev, 0);	deviceID = FLASH0_READ (dev, 2);	FLASH0_WRITE (dev, 0, 0xf0);	PRINTF ("flashProbe: vendor=0x%x device=0x%x\n", vendorID, deviceID);	if (vendorID == dev->vendorID && deviceID == dev->deviceID)		rv = OK;	else		rv = ERROR;  DONE:	PRINTF ("flashProbe: rv=%d\n", rv);	return rv;}PRIVATE int flashWait (flash_dev_t * dev, int addr, int expect, int erase){	int rv = ERROR;	int i, data;	int polls;#if 0	PRINTF ("flashWait: dev=%d addr=0x%x expect=0x%x erase=%d\n",		DEV_NO (dev), addr, expect, erase);#endif	if (dev->bank != FLASH0_BANK) {		rv = ERROR;		goto done;	}	if (erase)		polls = FLASH_ERASE_SECTOR_TIMEOUT;	/* Ticks */	else		polls = FLASH_PROGRAM_POLLS;	/* Loops */	for (i = 0; i < polls; i++) {		if (erase)			udelay (SLEEP_DELAY);		data = FLASH0_READ (dev, addr);		if (((data ^ expect) & 0x80) == 0) {			rv = OK;			goto done;		}		if (data & 0x20) {			/*			 * If the 0x20 bit has come on, it could actually be because			 * the operation succeeded, so check the done bit again.			 */			data = FLASH0_READ (dev, addr);			if (((data ^ expect) & 0x80) == 0) {				rv = OK;				goto done;			}			printf ("flashWait: Program error (dev: %d, addr: 0x%x)\n",					DEV_NO (dev), addr);			flashReset (dev);			rv = ERROR;			goto done;		}	}	printf ("flashWait: Timeout %s (dev: %d, addr: 0x%x)\n",		erase ? "erasing sector" : "programming byte",		DEV_NO (dev), addr);  done:#if 0	PRINTF ("flashWait: rv=%d\n", rv);#endif	return rv;}/*********************************************************************** * * Public Flash Routines * ***********************************************************************/STATUS flashLibInit (void){	int i;	PRINTF ("flashLibInit: devices=%d\n", flashDevCount);	for (i = 0; i < flashDevCount; i++) {		flash_dev_t *dev = &flashDev[i];		/*		 * For bank 1, probe both without and with byte swappage,		 * so that this module works on both old and new Mousse boards.		 */		flashReset (dev);		if (flashProbe (dev) != ERROR)			dev->found = 1;		flashReset (dev);		if (flashProbe (dev) != ERROR)			dev->found = 1;		dev->swap = 0;		if (dev->found) {			PRINTF ("\n  FLASH %s[%d]: iobase=0x%x - %d sectors %d KB",				flashDev[i].name, i, flashDev[i].base,				flashDev[i].sectors,				(flashDev[i].sectors * FLASH_SECTOR_SIZE) / 1024);		}	}	flashLibInited = 1;	PRINTF ("flashLibInit: done\n");	return OK;}STATUS flashEraseSector (flash_dev_t * dev, int sector){	int pos, addr;	PRINTF ("flashErasesector: dev=%d sector=%d\n", DEV_NO (dev), sector);	if (flashCheck (dev) == ERROR)		return ERROR;	if (sector < 0 || sector >= dev->sectors) {		printf ("flashEraseSector: Sector out of range (dev: %d, sector: %d)\n", DEV_NO (dev), sector);		return ERROR;	}	pos = FLASH_SECTOR_POS (dev, sector);	if (dev->bank != FLASH0_BANK) {		return ERROR;	}	addr = pos;	FLASH0_WRITE (dev, 0xaaa, 0xaa);	FLASH0_WRITE (dev, 0x555, 0x55);	FLASH0_WRITE (dev, 0xaaa, 0x80);	FLASH0_WRITE (dev, 0xaaa, 0xaa);	FLASH0_WRITE (dev, 0x555, 0x55);	FLASH0_WRITE (dev, addr, 0x30);	return flashWait (dev, addr, 0xff, 1);}/* * Note: it takes about as long to flash all sectors together with Chip * Erase as it does to flash them one at a time (about 30 seconds for 2 * MB).  Also since we want to be able to treat subsets of sectors as if * they were complete devices, we don't use Chip Erase. */STATUS flashErase (flash_dev_t * dev){	int sector;	PRINTF ("flashErase: dev=%d sectors=%d\n", DEV_NO (dev), dev->sectors);	if (flashCheck (dev) == ERROR)		return ERROR;	for (sector = 0; sector < dev->sectors; sector++) {		if (flashEraseSector (dev, sector) == ERROR)			return ERROR;	}	return OK;}/* * Read and write bytes */STATUS flashRead (flash_dev_t * dev, int pos, char *buf, int len){	int addr, words;	PRINTF ("flashRead: dev=%d pos=0x%x buf=0x%x len=0x%x\n",		DEV_NO (dev), pos, (int) buf, len);	if (flashCheck (dev) == ERROR)		return ERROR;	if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {		printf ("flashRead: Position out of range "			"(dev: %d, pos: 0x%x, len: 0x%x)\n",			DEV_NO (dev), pos, len);		return ERROR;	}	if (len == 0)		return OK;	if (dev->bank == FLASH0_BANK) {		addr = pos;		words = len;		PRINTF ("flashRead: memcpy(0x%x, 0x%x, 0x%x)\n",			(int) buf, (int) FLASH0_ADDR (dev, pos), len);		memcpy (buf, FLASH0_ADDR (dev, addr), words);	}	PRINTF ("flashRead: rv=OK\n");	return OK;}STATUS flashWrite (flash_dev_t * dev, int pos, char *buf, int len){	int addr, words;	PRINTF ("flashWrite: dev=%d pos=0x%x buf=0x%x len=0x%x\n",		DEV_NO (dev), pos, (int) buf, len);	if (flashCheck (dev) == ERROR)		return ERROR;	if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {		printf ("flashWrite: Position out of range "			"(dev: %d, pos: 0x%x, len: 0x%x)\n",			DEV_NO (dev), pos, len);		return ERROR;	}	if (len == 0)		return OK;	if (dev->bank == FLASH0_BANK) {		unsigned char tmp;		addr = pos;		words = len;		while (words--) {			tmp = *buf;			if (~FLASH0_READ (dev, addr) & tmp) {				printf ("flashWrite: Attempt to program 0 to 1 "					"(dev: %d, addr: 0x%x, data: 0x%x)\n",					DEV_NO (dev), addr, tmp);				return ERROR;			}			FLASH0_WRITE (dev, 0xaaa, 0xaa);			FLASH0_WRITE (dev, 0x555, 0x55);			FLASH0_WRITE (dev, 0xaaa, 0xa0);			FLASH0_WRITE (dev, addr, tmp);			if (flashWait (dev, addr, tmp, 0) < 0)				return ERROR;			buf++;			addr++;		}	}	PRINTF ("flashWrite: rv=OK\n");	return OK;}/* * flashWritable returns TRUE if a range contains all F's. */STATUS flashWritable (flash_dev_t * dev, int pos, int len){	int addr, words;	int rv = ERROR;	PRINTF ("flashWritable: dev=%d pos=0x%x len=0x%x\n",			DEV_NO (dev), pos, len);	if (flashCheck (dev) == ERROR)		goto done;	if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {		printf ("flashWritable: Position out of range "			"(dev: %d, pos: 0x%x, len: 0x%x)\n",			DEV_NO (dev), pos, len);

⌨️ 快捷键说明

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